【问题标题】:Issue with photo upload to server and database照片上传到服务器和数据库的问题
【发布时间】:2019-02-20 22:19:02
【问题描述】:

我在我的网站上上传照片时遇到问题。

我在$fileName = basename($_FILES["file"]["name"]); 行收到最后一条错误消息(“请选择要上传的文件”),并且没有插入数据库或上传到服务器。

我想知道我的插入是否可能存在问题,因为我正在尝试插入文件名,然后通过执行“images/”加上文件名来插入 url。所有图片都将上传到 images 目录中,所以我要确保 URL 始终在前面有“images/”。

我不确定这里还有什么问题,但也许我错过了什么

这是表单和 php 脚本:

<form action="uploadImage.php" method="post" enctype="multipart/form-data">
  <div class="form-group">
    <label for="formControl">Upload Image</label>
    <input type="file" class="form-control-file" id="formControl">
    <input type="submit" name="fileUpload">
  </div>
</form>

$statusMsg = '';

// File upload path
$targetDir = "images/";
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);


if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
    // Allow certain file formats
    $allowTypes = array('jpg','png','jpeg','gif','pdf');
    if(in_array($fileType, $allowTypes)){
        // Upload file to server
        if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
            // Insert image file name into database
            $insert = $mysqlConn->query("INSERT into images (image_name, url) VALUES ('".$fileName."', 'images/".$fileName."'");
            if($insert){
                $statusMsg = "The file ".$fileName. " has been uploaded successfully.";
            }else{
                $statusMsg = "File upload failed, please try again.";
            } 
        }else{
            $statusMsg = "Sorry, there was an error uploading your file.";
        }
    }else{
        $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';
    }
}else{
    $statusMsg = 'Please select a file to upload.';
}

// Display status message
echo $statusMsg;

【问题讨论】:

  • 您的文件输入缺少名称:name='file'
  • 啊,我确实错过了。不幸的是,仍然收到消息“请选择要上传的文件”,但这次它没有说明该特定行
  • 你也修复了提交吗?

标签: php html mysql upload


【解决方案1】:

根据您的验证,您应该像这样修改您的 html 标记:

<input type="file" name="file" class="form-control-file" id="formControl">
<input type="submit" name="submit">

或将您的验证条件更改为

if (isset($_POST["fileUpload"]) && !empty($_FILES["file"]["name"])) {
   ...
}

无论如何,您的文件输入元素需要name="file"

【讨论】:

  • 好吧,我已经改变了,但我现在得到“move_uploaded_file(images/sofa4.jpg): failed to open stream: Permission denied”在if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){行上
  • 尝试改成:$targetDir = getcwd() . "/images/";,如果还是不行,请检查您的目录权限。
  • 我为“用户”设置了我的图像文件夹的完全访问权限,但仍然是 move_uploaded_file(C:\Websites/images/sofa1.jpg): failed to open stream: Permission denied in C:\Websites\uploadImage.php on line 21 Warning: move_uploaded_file(): Unable to move 'C:\Windows\Temp\phpA067.tmp' to 'C:\Websites/images/sofa1.jpg' in C:\Websites\uploadImage.php on line 21
  • 不太清楚实际问题,你可以参考这个:stackoverflow.com/questions/29127789/…
  • 如果我的回答解决了您的问题get an error message ("Please select a file to upload"),请接受。谢谢。
猜你喜欢
  • 2017-12-19
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多