views:

53

answers:

3

I am uploading files to a server using php and while the move_uploaded_file function returns no errors, the file is not in the destination folder. As you can see I am using the exact path from root, and the files being uploaded are lower than the max size.

$target = "/data/array1/users/ultimate/public_html/Uploads/2010/";  
//Write the info to the bioHold xml file.
$xml = new DOMDocument();
$xml->load('bioHold.xml');
$xml->formatOutput = true;
$root = $xml->firstChild;
$player = $xml->createElement("player");
$image = $xml->createElement("image");
$image->setAttribute("loc", $target.basename($_FILES['image']['name']));
$player->appendChild($image);
$name = $xml->createElement("name", $_POST['name']);
$player->appendChild($name);
$number = $xml->createElement("number", $_POST['number']);
$player->appendChild($number);
$ghettoYear = $xml->createElement("ghettoYear", $_POST['ghetto']);
$player->appendChild($ghettoYear);
$schoolYear = $xml->createElement("schoolYear", $_POST['school']);
$player->appendChild($schoolYear);
$bio = $xml->createElement("bio", $_POST['bio']);
$player->appendChild($bio);
$root->appendChild($player);
$xml->save("bioHold.xml");
//Save the image to the server.
$target = $target.basename($_FILES['image']['name']);
if(is_uploaded_file($_FILES['image']['tmp_name']))
    echo 'It is a file <br />';
if(!(move_uploaded_file($_FILES['image']['tmp_name'], $target))) {
    echo $_FILES['image']['error']."<br />";
}
else {
    echo $_FILES['image']['error']."<br />";
    echo $target;   
}

Any help is appreciated.

Eric R.

+1  A: 

You need to make sure that whoever is hosting your pages has the settings configured to allow you to upload and move files. Most will disable these functions as it's a sercurity risk.

Just email them and ask whether they are enabled.

Hope this helps.

Ash Burlaczenko
PS Have you tested the script on local machine.
Ash Burlaczenko
As of last year I was able to upload to the server. I will check with them to see.
Eric Reynolds
A: 

your calls to is_uploaded_file and move_uploaded_file vary. for is_uploaded_file you are checking the 'name' and for move_uploaded_file you are passing in 'tmp_name'. try changing your call to move_uploaded_file to use 'name'

Scott M.
`move_uploaded_file()` requires the server-side temp name as the source, which is in `tmp_name`. Changing it to `name` is wrong as that's the client-side original filename, which would cause the move to fail with 'no such file'.
Marc B
A: 

Most like this is a permissions issue. I'm going to assume you don't have any kind of direct shell access to check this stuff directly, so here's how to do it from within the script:

Check if the $target directory exists:

$target = '/data/etc....';
if (!is_dir($target)) {
    die("Directory $target is not a directory");
}

Check if it's writeable:

if (!is_writable($target)) {
    die("Directory $target is not writeable");
}

Check if the full target filename exists/is writable - maybe it exists but can't be overwritten:

 $target = $target . basename($_FILES['image']['name']);
 if (!is_writeable($target)) {
     die("File $target isn't writeable");
 }

Beyond that:

if(!(move_uploaded_file($_FILES['image']['tmp_name'], $target))) {
    echo $_FILES['image']['error']."<br />";
}

Echoing out the error parameter here is of no use, it refers purely to the upload process. If the file was uploaded correctly, but could not be moved, this will still only echo out a 0 (e.g. the UPLOAD_ERR_OK constant). The proper way of checking for errors goes something like this:

if ($_FILES['images']['error'] === UPLOAD_ERR_OK) {
    // file was properly uploaded
    if (!is_uploaded_File(...)) {
        die("Something done goofed - not uploaded file");
    }
    if (!move_uploaded_file(...)) {
        echo "Couldn't move file, possible diagnostic information:"
        print_r(error_get_last());
        die();

    }
} else {
    die("Upload failed with error {$_FILES['images']['error']}");
}
Marc B
So this was MOSTLY right. Issue explained: The folder "2010" that was in "Uploads" was writable so all of your checks passed without a problem. However, Uploads itself was not writable. Doing all these checks made me think about that. So when I made Uploads writable it worked beautifully. Thanks Marc.
Eric Reynolds