【问题标题】:my php upload script no result ?我的php上传脚本没有结果?
【发布时间】:2011-12-11 03:28:36
【问题描述】:

我的 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 连接工作 100%,我已经确定了,我的脚本文件夹中有一个名为“images”的文件夹,问题出在哪里?

【问题讨论】:

  • 基本调试告诉你什么? $_POST 包含什么(做一个print_r($_POST);?查询中是否有任何错误(使用echo mysql_error() 来查看它们?是否激活了错误报告(使用error_reporting(E_ALL); 来确保它是)?
  • 另外,您的数据库查询容易受到 SQL 注入 php.net/manual/en/security.database.sql-injection.php
  • 试过了,还是不行,问题是我只有白页没有消息,而且文件不在images文件夹中
  • 我很确定您没有激活错误报告。
  • 现在显示致命错误:在第 17 行调用 C:\AppServ\www\PromBom\add.php 中未定义的函数 mysql_connect()

标签: php mysql file file-upload


【解决方案1】:

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

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

错误代码是文档here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    相关资源
    最近更新 更多