kstev
kstev

Reputation: 740

PHP file upload / move_uploaded_file not working, not like other questions

I have looked through the five other similar questions on here and none of them have been able to solve my problem. I am uploading a CSV file that is 1657 bytes in size.

Here are some values from my php.ini file

upload_max_filesize = 2M

post_max_size = 8M

$file = $_FILES['csv'];

if (!isset($file)) {
    halt(HTTP_NOT_FOUND, 'This page could not be found on the web server');
}

if (substr(strrchr($file['name'], '.'), 1) != 'csv') {
    halt(500, 'Sorry but please upload a CSV file next time!');
}

if ($file['error'] != '0') {
    die('An error occurred during upload.<br />Error: ' . $file['error']);
}

$file_loc = '/home/x/tmp/up/' . basename($file['tmp_name']) . '.csv';

if (file_exists('/home/x/tmp/up/')) {
    echo 'the directory exists....<br/>';
}

if (file_exists($file['tmp_name'])) {
    echo 'tmp file exists<br/>';
    echo filesize($file['tmp_name']) . 'b<br/>';
}

if (move_uploaded_file($file['tmp_name'], $file_loc)) {
    echo 'Uploaded to: ' . $file_loc;
} else {
    echo 'something went wrong!' . print_r($_FILES['csv'], true);
    echo '<br />Upload Max Size: ' . ini_get('upload_max_filesize');
    echo '<br />POST Max Size: ' . ini_get('post_max_size');

}

And here is the generated output when I upload the file:

the directory exists....
tmp file exists
1657b
something went wrong!Array ( [name] => hm.csv [type] => application/vnd.ms-excel [tmp_name] => /tmp/php72p1VP [error] => 0 [size] => 1657 ) 
Upload Max Size: 2M
POST Max Size: 8M

I have already looked on PHP.net under the "common pitfalls" article which didn't lead to anything being fixed. Can anyone see what is wrong or give me some tips?

Upvotes: 2

Views: 3479

Answers (2)

kstev
kstev

Reputation: 740

After multiple exchanges with a tech at the hosting provider, they managed to give the folder the right permissions (I had tried 0777 before but it did not work). I believe some voodoo went on behind the scenes but this is No errors in the error_log; I have set 777 permissions on /home/x/up and the script began working.

Upvotes: 0

Waqar Alamgir
Waqar Alamgir

Reputation: 9968

The following tips can be helpful.

1. The folder exists, check for spellings
2. Check the properties of the folder and make sure the permissions have read+write 0666
3. Make sure the file is within your public html root, otherwise double check the owner of the file, and make sure PHP Has read / write access to it.
4. Look for the logs i.e. Unable to move '/tmp/php6wlOg1' to 'upload/110216104651_00134_smooth_1440x900.jpg'
5. If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty. So set it according to your need.

Also set this to see if there are any errors

error_reporting(E_ALL); // or E_STRICT
ini_set("display_errors",1);
ini_set("memory_limit","1024M");

Upvotes: 1

Related Questions