drj26159 2018-08-07 05:30
浏览 72
已采纳

图像文件上传无法在wamp服务器上运行

Here is my HTML code "post.html"

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="upload" id="upload">
<input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

and here is my PHP code "upload.php"

<?php
target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["upload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["upload"]["tmp_name"]);
if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
}
}
?>

and the result I am getting is

File is an image - image/jpeg.

There is no issue with execution of the files but the thing is whenever I uploads any file or image via html code the file does not comes up in the upload folder despite of successful execution of the code.

P.S I am using php7 on wampserver

  • 写回答

2条回答 默认 最新

  • doujiao2014 2018-08-07 05:35
    关注

    You should use below code line before your code line $uploadOk = 1;

    move_uploaded_file($_FILES["upload"]["tmp_name"], $target_file);

    Like:

    if (move_uploaded_file($_FILES["upload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["upload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
    

    While coping codes from some other sites please understand what is written in there and proceed. :-)

    $check = getimagesize($_FILES["upload"]["tmp_name"]); this line of code used to get size of image & do a size validation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)