PHP Error - Uploading a file

谁都会走 提交于 2019-12-18 03:57:25

问题


I'm trying to write some PHP to upload a file to a folder on my webserver. Here's what I have:

<?php
    if ( !empty($_FILES['file']['tmp_name']) ) {
        move_uploaded_file($_FILES['file']['tmp_name'], './' . $_FILES['file']['name']);
        header('Location: http://www.mywebsite.com/dump/');
        exit;
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
    <head>
        <title>Dump Upload</title>
    </head>
    <body>
        <h1>Upload a File</h1>
        <form action="upload.php" enctype="multipart/form-data" method="post">
            <input type="hidden" name="MAX_FILE_SIZE" value="1000000000" />
            Select the File:<br /><input type="file" name="file" /><br />
            <input type="submit" value="Upload" />
        </form>
    </body>
</html>

I'm getting these errors:

Warning: move_uploaded_file(./test.txt) [function.move-uploaded-file]: failed to open stream: Permission denied in E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php on line 3

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\Temp\phpA30E.tmp' to './test.txt' in E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php on line 3

Warning: Cannot modify header information - headers already sent by (output started at E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php:3) in E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php on line 4

PHP version 4.4.7 Running IIS on a Windows box. This particular file/folder has 777 permissions.

Any ideas?


回答1:


As it's Windows, there is no real 777. If you're using chmod, check the Windows-related comments.

Check that the IIS Account can access (read, write, modify) these two folders:

E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\
C:\WINDOWS\Temp\



回答2:


OMG

move_uploaded_file($_FILES['file']['tmp_name'], './' . $_FILES['file']['name']);

Don't do that. $_FILES['file']['name'] could be ../../../../boot.ini or any number of bad things. You should never trust this name. You should rename the file something else and associate the original name with your random name. At a minimum use basename($_FILES['file']['name']).




回答3:


Try adding a path. The following code works for me:

<?php

if ( !empty($_FILES['file']) ) {
    $from = $_FILES['file']['tmp_name'];
    $to = dirname(__FILE__).'/'.$_FILES['file']['name'];

    if( move_uploaded_file($from, $to) ){
        echo 'Success';   
    } else {
        echo 'Failure';   
    }

    header('Location: http://www.mywebsite.com/dump/');
    exit;
}
?>



回答4:


Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\Temp\phpA30E.tmp' to './people.xml' in E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php on line 3

is the important line it says you can't put the file where you want it and this normally means a permissions problem

check the process running the app (normally the webservers process for php) has the rights to write a file there.

EDIT:

hang on a bit I jumped the gun a little is the path to the file in the first line correct?




回答5:


Another think to observe is your directory separator, you are using / in a Windows box..




回答6:


Add the IIS user in the 'dump' folders security persmissions group, and give it read/write access.




回答7:


Create a folder named "image" with folder permission 777

<?php
    move_uploaded_file($_FILES['file']['tmp_name'],"image/".$_FILES['file']['name']);
?>



回答8:


We found using below path

{['DOCUMENT_ROOT'] + 'path to folder'

and giving everyone full access to the folder resolved the issue.

Make sure to not reveal the location in the address bar. No sense in giving the location away.



来源:https://stackoverflow.com/questions/3611/php-error-uploading-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!