开发者

Trouble with uploading a file

I can't get any files to upload successfully, it's just going to echo 'error';

HTML:

<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value = "2000000">
    Upload this file: <input name ="userfile" type="file">
    <input type="submit" value="Send File">
</form>

PHP:

<?php
if ($_FILES['userfile']['error']>0)
{
   开发者_高级运维 echo 'Problem.';
    exit;
}

$upfile='/uploads/'.$_FILES['userfile']['name'];

if (is_uploaded_file($_FILES['userfile']['name']))
{
    if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
    {
        echo 'Problem: could not move file';
        exit;      
    }
}
else
{
    echo 'Error';
    exit;
}

echo 'File uploaded successfully.';
?>

I'm sure it's something simple I'm messing up, but I've spent about an hour trying to find it. Thanks.


if (is_uploaded_file($_FILES['userfile']['tmp_name'])) 

tmp_name instead of name


$_FILES['userfile']['tmp_name'] is the name of the uploaded file. $_FILES['userfile']['name'] is just the name that the file had when it was on the computer of the user.


For proper working, the function is_uploaded_file() needs an argument like $_FILES['userfile']['tmp_name'], - the name of the uploaded file on the client's machine $_FILES['userfile']['name'] does not work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜