在上传和重命名临时文件后,很难用php移动它们


Difficulty in moving temp files with php after uploading them and renaming

我有一个用于上传文件到web服务器的表单,然后保存文件路径以及它连接到的电子邮件地址和一个sql表的串行代码。问题是,如果有人上传了与前一个人或上传的同名文件,该怎么办?为了解决这个问题,我想将文件命名为"插入串行代码"。mp4或。pdf。当我这样做时,它会出现以下错误:

Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in /home/content/98/10339998/html/scripts/upload.php on line 57
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php7UzcdG' to '../story_files/' in /home/content/98/10339998/html/scripts/upload.php on line 57
Stored in: ../story_files/

下面是一个完整的echo示例:

Customer's unique serial code: d0d74-ef227 

Upload: 
Type: application/pdf
Size: 14.4287109375 kB
Temp file: /tmp/php7UzcdG
Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in /home/content/98/10339998/html/scripts/upload.php on line 57
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php7UzcdG' to '../story_files/' in /home/content/98/10339998/html/scripts/upload.php on line 57
Stored in: ../story_files/
../story_files/
Upload: file.mp4
Type: video/mp4
Size: 374.6396484375 kB
Temp file: /tmp/phpQczVLm
file.mp4 already exists. 
Serial Code Emailed to Customer Michael
这里是代码,注意视频和故事部分的区别,我没有转换视频部分,但它是完全工作的原始代码。另外,PHP认为串行代码是一个文件路径,为什么呢?代码只是由10位的字母和数字组成的两组5位数字,中间有一个破折号以简化阅读。
    <title>Uploading Files and Sending Serial Code To Customer</title>
<!--Favicon Code-->
<link rel="shortcut icon" type="image/x-icon" href="../images/favicon.ico" />
<!--Favicon Code-->

<link href="/CSS/CSS.css" rel="stylesheet" type="text/css" />
<div align="center">
  <p><span class="linkText"><a href="/index.html">Home</a> <a href="/contact-us.php">Contact Us</a> <a href="/products.html">Products</a><a href="/products.html"></a><a href="/products.html"></a></span> </p>
  <p>&nbsp;</p>
  <h2 class="headingText"><img src="/images/banner.jpg" alt="legendmaker - makes legends: banner" width="297" height="473"></h2>
  <h2 class="headingText">&nbsp;</h2>
  <h2 class="headingText">Upload Story Files</h2>
</div>
<?php
// before we do anything make sure there is a correct password (we don't want unauthorized usage)
$password = trim($_POST['password']);
if ("*****" == $password)// not going to post my pass on the forums :P
{

///// generate unique serial code ///////////////////////////////////////////////////////////////
$string1 = substr(md5(uniqid(rand(), true)), -5, 5);
$string2 = substr(md5(uniqid(rand(), true)), -5, 5);
$serial  = sprintf('%s-%s', $string1, $string2);
echo "Customer's unique serial code:  ";
echo $serial;
echo "     ";
/////end of generate unique serial code ///////////////////////////////////////////////////////////////
///story file upload///////////////////////////////////////////////////////////////////////////////////
if ($_FILES["file"]["size"])
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "<br>" . "<br>" . "<br>" . "Upload: " . $_FILES["file"]["$code"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    if (file_exists("stories/" . $_FILES["file"]["$code"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../story_files/" . $_FILES["file"]["code"]);
      echo "Stored in: " . "../story_files/" . $_FILES["file"]["$code"] . "<br>";
      $storyPath  = sprintf("../story_files/" . $_FILES["file"]["$code"]);
      echo $storyPath;    
      }
    }
  }
else
  {
  echo "No story file was chosen, this is not an error just a notification. Any other files selected will still upload correctly.   ";
  }


///video file upload////////////////////////////////////////////////////////////////////////////////

if ($_FILES["videoFile"]["size"])
  {
  if ($_FILES["videoFile"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["videoFile"]["error"] . "<br>";
    }
  else
    {
    echo "<br>" . "<br>" ."Upload: " . $_FILES["videoFile"]["name"] . "<br>";
    echo "Type: " . $_FILES["videoFile"]["type"] . "<br>";
    echo "Size: " . ($_FILES["videoFile"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["videoFile"]["tmp_name"] . "<br>";
    if (file_exists("../video_files/" . $_FILES["videoFile"]["name"]))
      {
      echo $_FILES["videoFile"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["videoFile"]["tmp_name"],
      "../video_files/" . $_FILES["videoFile"]["name"]);
      echo "Stored in: " . "../video_files/" . $_FILES["videoFile"]["name"] . "<br>";     
      $videoPath  = sprintf("../video_files/" . $_FILES["file"]["name"]);
      echo $videoPath;
      }
    }
  }
else
  {
  echo "No video file was chosen, this is not an error just a notification. Any other files selected will still upload correctly.   ";
  }  
  /// submit information to database/////////////////////////////////////////////////////////////////////
$username="storycodes";
$password="Legendmaker1!";
    $con = mysqli_connect("storycodes.db.10339998.hostedresource.com",$username,$password);
    if (!$con)
    {
        die('Could not connect: ' . mysqli_error());
    }
$email = $_POST['email'] ;  
$submitInformation = "INSERT INTO `storycodes`.`storycodes` (`code`, `email`, `video`, `story`) VALUES ('$serial', '$email', '$videoPath', '$storyPath');"; 
mysqli_query($con, $submitInformation); // submits information to database for later recollection
////////////////////end of submit to database///////////////////////////////////////////////////////////

/////////////////// email the code to customer//////////////////////////////////////////////////////////
require_once '../PHPMailer_5.2.2/class.phpmailer.php';
$name = $_POST['name'] ;

$body = "Thank you for using legendmaker $name! Your story has been completed and will now be accesible at www.thelegendmaker.net/stories.html  On that page you will be required to enter a serial code in order to access your files. We require a serial code in order to access files because we care about our customers and security is a concern.                                                           Your serial code is: $serial";
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try 
{
  $mail->AddAddress($email, $name);
  $mail->SetFrom('fakeemail1@gmail.com', 'Sender Name');
  $mail->AddReplyTo('fakeperson@yahoo.com', 'Fake Name');
  $mail->Subject = "Message From Legendmaker: $name your story is now complete.";
  $mail->Body = $body;
  $mail->Send();
  echo "<br>" . "<br>" . "Serial Code Emailed to Customer $name</p>'n";
  echo "<img src='/images/knight-success.png' alt='success' width='429' height='791' />"; 
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}
/////////////////// end of email code///////////////////////////////////////////////////////////////////
}
else
{
echo "incorrect password";
}
/// end of file uploads//////////////////////////////////////////////////////////////////////////
?>
<link href="/CSS/CSS.css" rel="stylesheet" type="text/css" />

<p>
  <!--google cart code ------------------------------------>
  <script  id='googlecart-script' type='text/javascript' src='https://checkout.google.com/seller/gsc/v2_2/cart.js?mid=215313740482542' integration='jscart-wizard' post-cart-to-sandbox='false' currency='USD' productWeightUnits='LB'></script>
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p align="center"><a href="/contact-us.php">Contact Us</a> <a href="/upload.html">Site Owner Upload</a> <a href="/service_agreement.html">Service Agreement</a></p>

您使用$_FILES["file"]["code"]而不是$_FILES["file"]["name"]作为不存在的文件名,因此结果是您正在传递一个目录作为第二个参数。

move_uploaded_file($_FILES["file"]["tmp_name"],
  "../story_files/" . basename($_FILES["file"]["name"]));

编辑如果您想使用生成的串行代码作为文件名,只需将其连接到move_uploaded_file

中的目录字符串。
move_uploaded_file($_FILES["file"]["tmp_name"],
  "../story_files/" . $serial);