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

标签 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”的文件夹,问题出在哪里?

最佳答案

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

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

错误代码是文档 here

关于php - 我的 php 上传脚本没有结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7825433/

相关文章:

mysql - 表错误 :1452 Cannot add or update a child row: a foreign key?

file - Ansible - 复制是否支持 shell * 扩展

php - 在 Web 应用程序中使用 TensorFlow 模型

mysql:获取具有给定类别的总行的单个结果的行索引

javascript - 理解连接字符串的三元运算符

php - 使用 PHP 更新 mysql 数据库表

Python - 如何在文本文件中制作字典?

ios - 如何在 iOS 中搜索特定文本的文件名

php - 通过 cURL 提交表单并将浏览器重定向到 PayPal

php - 我的应用程序正在向服务器发送 XML 文件 : need some guidance on writing a PHP script to parse the XML file