Simple Upload script not working

Hi there,

I’m trying to get a file uploader script to work. Ideally I’ll be using a flash interface for multiple file uploading, but for the moment I’m starting with a simple single file uploader, which the user will access via a simple html page. This has been uploaded to my server but for some reason it’s not working. I’ve checked with my hosting company to ensure that the permissions have been set correctly via their control panel, and it is world readable/writable.

The html form code looks like this:

<form enctype=“multipart/form-data” action=“uploader.php” method=“POST”>
<input type=“hidden” name=“MAX_FILE_SIZE” value=“1000000” />
Choose a file to upload: <input name=“uploadedfile” type=“file” /><br />
<input type=“submit” value=“Upload File” />
</form>

The PHP script looks like this:

<?php

$target_path = “clientuploads/”;

$target_path = chmod($target_path . basename( $_FILES[‘uploadedfile’][‘name’]), 0777);

if(move_uploaded_file($_FILES[‘uploadedfile’][‘tmp_name’], $target_path)) {
echo "The file “. basename( $_FILES[‘uploadedfile’][‘name’]).
" has been uploaded”;
} else{
echo “There was an error uploading the file, please try again!”;
}

?>

My main concern is with the php script and whether the permissions are correct (ie. chmod & 0777), and whether I’ve used the correct relative path for $target_path.

Ultimately I would like to use the php script to create a new directory path underneath “clientuploads” based upon their login details (ie. session data for username and password)

Something like this:

$company = $_SESSION[‘Company’];
$branch = $_SESSION[‘Branch’];

//what is the directory path I want to upload to?
$target_path = “clientUploads/”."$company/"."$branch/";

//create the directory (should have write permissons)
$old_umask = umask(0);
mkdir($target_path, 0777);
umask($oldumask);

Again, unsure about the correct relative path to use.

Any feedback about what could be preventing it from working would be welcomed.

Thanks.

Well, it looks like you are doing several things wrong.
First, you try to chmod something that doesn’t exist.

$target_path = chmod($target_path . basename( $_FILES[‘uploadedfile’][‘name’]), 0777);

Your file is not uploaded yet so what to chmod?

Second, you set the target path to have the value returnet by chmod (it returns bool). After this, you try to upload the file

if(move_uploaded_file($_FILES[‘uploadedfile’][‘tmp_name’], $target_path))

but your $target_path variable now holds the chmod result.

To make this work, move the chmod line after the upload. It should do the trick.