使用filesize()函数时出错

时间:2013-12-17 03:28:22

标签: php

我正在编写一个图像上传器来使用我的php库。我正在限制用户可以上传的文件类型和文件大小。我使用filesize()函数收到错误。我省略了完整路径并将其替换为路径。这是错误:

Notice: Undefined index: uploaded in path/upload.php on line 19 Notice: Undefined index: uploaded in path/upload.php on line 54 Sorry, there was a problem uploading your file. 

当我进入照片目录时,照片会上传并显示在我的图库页面上。有谁知道为什么我收到此错误以及如何解决它?这是代码:

<html>
<head><title>File Manager!</title></head>

<body>

<form enctype="multipart/form-data" action="upload.php" method="POST">
 Please choose a file: <input name="uploaded" type="file" /><br />
 <input type="submit" value="Upload" />
 </form>

</body>

</html>

<?php 


$targetDir = "/students/jmartz/public_html/130a/PhotoDir/";
$targetDir = $targetDir . basename( $_FILES['uploaded']['name']); //Line 19
$goodFile=true;; 

//Line 22
$fileSize = filesize($targetDir);
if ($fileSize > 250000) 
{ 
    echo "Your file is too large.<br>"; 
    $goodFile = false;

}



$finfo = finfo_open(FILEINFO_MIME_TYPE);
$fileType = finfo_file($finfo, $targetDir);



$fileType = filetype($targetDir); 

//echo "File type: $fileType";
if ($fileType =="text/x-php") 
{ 
    echo "No PHP files<br>"; 
    $goodFile = false;
}    



if ($goodFile == false) 
{
    echo "Sorry your file was not uploaded"; 
}  
else 
{
    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $targetDir)) //Line 54
    { 
        echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded"; 
    } 
    else 
    { 
        echo "Sorry, there was a problem uploading your file."; 
    } 
} 
?>

2 个答案:

答案 0 :(得分:0)

你说错误是使用filesize()但是第22行,错误是第19行和$_FILES语句。

使用'file'代替'uploaded'

$_FILES['file']['name']

答案 1 :(得分:0)

尝试本教程可能会有所帮助

<强>参考

http://blog.trofeosolution.com/index.php/blog/file-upload