开发者

Having trouble uploading a file

I am having trouble uploading a file. First of all I have a class:

class upload

{
private $name;
private $document;
public function __construct($nme,$doc)
{
    $this->setName($nme);
    $this->setDocument($doc);
}
public function setName($nme)
{
    $this->name = $nme;
}
public function setDocument($doc)
{
    $this->document = $doc;
}
public function fileNotPdf()
{
    /* Was the file a PDF? */
    if ($this->document['type'] != "application/pdf") 
        {
            return true;
        }
        else
        {
            return false;
        }
}
public function fileNotUploaded()
{
    /* Make sure that the file was POSTed. */
    if (!(is_uploaded_file($this->document['tmp_name'])))
    {
        return true;
    }
    else
    {
        return false;
    }
}
public function fileNotMoved($repositry)
{
        /* move uploaded file to final destination. */
        $result = move_uploaded_file($this->document['tmp_name'],
        "$repositry/$this->name.pdf开发者_开发技巧");
        
        if($result)
        {
            return false;
        }
        else
        {
            return true;
        }
}
}

Now for my main page:

$docName = $_POST['name'];

$page = $_FILES['doc'];

if($_POST['submit'])

{
/* Set a few constants */
$filerepository = "np";
$uploadObj = new upload($docName, $page);
if($uploadObj->fileNotUploaded()) 
{
    promptUser("There was a problem uploading the file.","");
}
elseif($uploadObj->fileNotPdf())
{
    promptUser("File must be in pdf format.","");
}
elseif($uploadObj->fileNotMoved($filerepository))
{
    promptUser("File could not be uploaded to final destination.","");
}
else
{       
    promptUser("File has been successfully uploaded.","");
}
}

The errors that I get:

Warning: move_uploaded_file(about.pdf)[function.move-uploaded-file]: failed to open stream: No such file or directory in...

Warning: move_uploaded_file()[function.move-uploaded-file]: Unable to move 'c:\xampp\tmp\php13.tmp' to 'about.pdf' in...

File could not be uploaded to final destination.


The manual says:

If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.

This is the only case when, according to the manual, a warning is issued, so I assume that this is what happens when you try to move the file.

move_uploaded_file() is both safe mode and open_basedir aware. However, restrictions are placed only on the destination path as to allow the moving of uploaded files in which filename may conflict with such restrictions. move_uploaded_file() ensures the safety of this operation by allowing only those files uploaded through PHP to be moved.

My conclusion: There is no error in your code, your PHP configuration does not allow you to move the file to your target directory. You should research the limits imposed by safe mode and open_basedir to see why exactly the move operation fails.


If I upload a file as "my_file", "$repositry/$this->name.pdf" will evaluate to np/my_file.pdf. That would put your target directory inside the tmp directory. The tmp directory should already exist and be writable, so make sure that the np directory exists inside the tmp directory. The move_uploaded_file() function will not create it automatically if it's missing. With the np directory missing, I was getting the same errors you are.

Also note that you shouldn't save uploaded files to a subdirectory of the tmp directory. I use XAMPP and when I run your code the uploaded file is moved to tmp/np and then deleted immediately.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜