duan2891 2014-04-24 18:51
浏览 57
已采纳

文件未上传到目录

I'm using a form for uploading an image to my img folder. The error code of the photo array is 0, that means that there isn't any error and the file was uploaded successfully, but it wasn't... This is the code I have:

This is the HTML:

<form enctype="multipart/form-data" method="post" action="upload.php">
    <label>Thumbnail:</label>
    <input name="thumbnail" type="file">
    <input type="submit">
</form>

This is the upload.php file:

<?php
$uploaddir = 'img/';
$uploadfile = $uploaddir . basename($_FILES['thumbnail']['name']);
echo $uploadfile;

echo '<pre>';
if (move_uploaded_file($_FILES['thumbnail']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.
";
} else {
    echo "Possible file upload attack!
";
}

echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>

I've put a print_r($_FILES) because I want to see the array... This is what I get after uploading the picture:

Array
(
    [thumbnail] => Array
        (
            [name] => Stack.png
            [type] => image/png
            [tmp_name] => /tmp/phpEo6Gu5
            [error] => 0
            [size] => 482278
        )

)

And, if you look here, you can see that error 0 corresponds to:

There is no error, the file uploaded with success.

But I don't have any files in my folder... This is the tree-view of the folders:

|test
|||img (folder)
|||||somephoto.png
|||||otherphoto.png
|||upload.php

But there isn't any photo in there because I can't upload them... What am I doing wrong?

Thanks!

EDIT: I know there are a lot of security risks here, but I'm only testing in my local machine, later, if it works, I'll add all the security-related code.

  • 写回答

2条回答 默认 最新

  • dql1978dql1978 2014-04-24 18:55
    关注

    The file is uploaded to a temporary folder, as print_r($_FILES) showed, the actual file is /tmp/phpEo6Gu5.

    You have to use move_uploaded_file() to move this file to its final destination. Example:

    move_uploaded_file($_FILE['thumbnail']['tmp_name'], 'img/final_name.png');
    

    Do not trust $_FILE['thumbnail']['name'] or $_FILE['thumbnail']['type'] as safe and valid, because this comes from client-side and can be twisted by an ill-intentioned user to exploit security issues in your site.

    In addition, make sure the user account your script runs with has permission to write to the final directory. Easy way to solve that would be running chmod 777 img in your shell (gives full read+write+execute permission to everyone in folder img).

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

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常