【问题标题】:what am i doing wrong in the upload script我在上传脚本中做错了什么
【发布时间】:2013-12-08 08:25:14
【问题描述】:

我正在尝试创建一个添加随机编号的脚本。在上传的文件中,然后上传到服务器

   //This line assigns a random number to a variable. You could also use a timestamp here if you prefer. 
 $ran = rand () ;

 //This takes the random number (or timestamp) you generated and adds a _ on the end, so it is ready of the file extension to be appended.
 $ran2 = $ran."_";

 //This gets all the other information from the form 
 $name=$_POST['name']; 
 $email=$_POST['email']; 
 $phone=$_POST['phone']; 
 $pic=$ran2.$_FILES['uploaded']['name'];

 //escape User Input to help prevent SQL Injection
 $name= mysql_real_escape_string($name);   
 $email= mysql_real_escape_string($email);
 $phone= mysql_real_escape_string($phone);
 $pic= mysql_real_escape_string($pic);

 // Connects to your Database 
 mysql_connect("example.com", "user", "password") or die(mysql_error()) ; 
 mysql_select_db("database") or die(mysql_error()) ; 

 //Writes the information to the database 
 mysql_query("INSERT INTO `table` VALUES ('$name', '$email', '$phone', '$pic')") ; 



 //This assigns the subdirectory you want to save into... make sure it exists!
 $target = "images/";


//This combines the directory, the random file name, and the extension 
$target = $target . $pic; 


 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok
 echo "file uploaded in ".$target; 
 } 
 else { 

 //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file."; 
 } 

虽然我认为问题出在 move_uploaded_file 或 $pic=$ran2.$_FILES['uploaded']['name']。请帮我改正。

【问题讨论】:

  • 请张贴您的表格!

标签: php upload move


【解决方案1】:

使用复制功能而不是 move_uploaded_file 来复制和重命名上传的文件。

copy($_FILES['uploaded']['tmp_name'], $target);

【讨论】:

  • 不,告诉文件名是空的。
  • 请发布您的 html 表单代码。输入名称是否“已上传”?
  • 非常感谢,表单有问题,但如果没有您的复制功能帮助,就不会发生这种情况。谢谢
猜你喜欢
  • 2020-02-04
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
  • 2021-03-24
  • 2019-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多