我的php上传脚本没有结果?

时间:2011-10-19 17:28:39

标签: php mysql file file-upload

我的php文件上传脚本有问题,我正在尝试上传文件,然后在mysql数据库上存储一个位置。

问题是当我尝试上传任何文件时,action.php不会给我任何关于它是成功还是失败的消息!

这是我的html表单:

<form enctype="multipart/form-data" action="add.php" method="POST"> 
 Name: <input type="text" name="name"><br> 
 E-mail: <input type="text" name = "email"><br> 
 Phone: <input type="text" name = "phone"><br> 
 Photo: <input type="file" name="photo"><br> 
 <input type="submit" value="Add"> 
 </form>

这是我的add.php代码:

     <?php 

 //This is the directory where images will be saved 
 $target = "/images/"; 
 $target = $target . basename( $_FILES['photo']['name']); 

 //This gets all the other information from the form 
 $name=$_POST['name']; 
 $email=$_POST['email']; 
 $phone=$_POST['phone']; 
 $pic=($_FILES['photo']['name']); 

 // Connects to your Database 
 mysql_connect("localhost", "root", "123123") or die(mysql_error()) ; 
 mysql_select_db("bom") or die(mysql_error()) ; 

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

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

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else { 

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

mysql conntection工作100%,我已经确定了,我的脚本文件夹中还有一个名为“images”的文件夹,问题出在哪里?

1 个答案:

答案 0 :(得分:1)

您认为文件上传确实有效。在对上传执行任何操作之前,您应该至少进行一些最小的错误处理:

if ($_FILES['uploadedfile']['error'] !== UPLOAD_ERR_OK) {
    die("Upload failed with error code " . $_FILES['uploadedfile']['error'])
}

错误代码为文档here