0

I have written a script in PHP which is supposed to enable yuo to upload files. However, it can't seem to move the file to the right folder after it has uploaded it. Can anyone see what the problem is?

Here is the code:

    

    echo 'This is upload.php';
    $target_path = 'wwwroot/rsfleet.co.uk/ipad_test/sub/';
    echo 'Size of file: ';
    echo $_FILES['userfile']['size'];
    $target_path = $target_path . basename($_FILES['userfile']['name']);
    echo $target_path; 
    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
        echo 'File uploaded OK.';
    } else {
        echo 'Problem with file upload.';
    }
    if (move_uploaded_file(basename($_FILES['userfile']['tmp_name']), $target_path)) {
        echo 'The file ' . basename($_FILES['userfile']['name']) . ' has been moved';
    } 
    else 
    {
        echo 'Error moving file. Please try again.';
    }
    echo 'End of page.';

    
2
  • Have you looked in your server log to see if there are any error messages? Commented Oct 4, 2011 at 14:22
  • 1
    Have you checked the server/PHP logs? Does the server/PHP process have write permissions on the directory you're trying to move the uploaded file to? Commented Oct 4, 2011 at 14:23

4 Answers 4

2
move_uploaded_file(basename($_FILES['userfile']['tmp_name']), ...)

This cannot work. It would only work if the temporary file was stored in the working directory (protip: it's not). Use the following instead:

move_uploaded_file($_FILES['userfile']['tmp_name'], ...)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help. By the way, how can I look in the server log? It is a Windows server.
As well, $target_path is probably wrong too. Looks like you're trying to do an absolute path, but forgot the leading /.
0

Change

(move_uploaded_file(($_FILES['userfile']['tmp_name']),

to

(move_uploaded_file(($_FILES['userfile']['tmp_name']),

and it'll probably work

because $_FILES['userfile']['tmp_name'] has already full path

4 Comments

Thanks for your help, everyone. Sadly, it's still not working. Sometimes, I get error 500. Does anyone know how I can view the server log?
@user947185: /var/log/apache2/apache2.log
@user947185: btw targetpath should start with /
I have managed to find the log file. It contains entries like this one: 2011-10-04 14:09:23 POST /ipad_test/upload.php 82.36.128.237 HTTP/1.1 Mozilla/5.0+(Windows;+U;+Windows+NT+6.1;+en-GB;+rv:1.9.2.8)+Gecko/20100722+Firefox/3.6.8 rsfleet.co.uk/ipad_test/upload.html www.rsfleet.co.uk 200 0 365 Does this shed any light on the problem?
0

Check that the folder has the correct permissions, these are called a CHMOD command.

In FTP programs like Filezilla, you can set these permission by right mouse clicking on the folder and clicking 'File Permissions...'

Comments

0

I suppose problem is here:

$target_path = 'wwwroot/rsfleet.co.uk/ipad_test/sub/';

change to

$target_path = '/wwwroot/rsfleet.co.uk/ipad_test/sub/';

but it's just assumption.

also, basename is not necessary when moving files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.