上传和重命名后用PHP移动临时文件很困难

时间:2013-03-26 05:35:49

标签: php mysql sql file file-upload

我有一个表单,用于将文件上传到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/

这是一个完整的回声示例:

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>

1 个答案:

答案 0 :(得分:0)

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

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

EDIT  如果你想使用生成的序列代码作为文件名,只需将它连接到move_uploaded_file

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