2
votes

I am trying to implement a file upload to a pre-existing form that I know works, and still does aside from the file upload section.

Essentially, I am unsure of the error and how to fix it. The outputs are only what I have put in there based on my understanding of the code

I get the following output from the below code:

OUTPUT

Array
(
    [chart-image] => Array
        (
            [name] => Chart example.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/php5iGbQD
            [error] => 0
            [size] => 50222
        )

)
CAN'T MOVE FILE

FORM

<form id="<?php echo $type ?>-trade" enctype="multipart/form-data" method='post' action='<?php echo $url; ?>'>
*snip*
    <input type="hidden" name="MAX_FILE_SIZE" value="52428800" />    
    <input name="chart-image" type="file" />
*snip*
</form>

ACTION PAGE

snip
 print_r($_FILES);

if($_FILES['chart-image']['error'] == '0'){
    $uploaddir = '/images/charts/';
    $file = basename($_FILES['chart-image']['name']);

    $uploadfile = $uploaddir . $file;
    if(file_exists($_FILES['chart-image']['tmp_name'])){
       if (move_uploaded_file($_FILES['chart-image']['tmp_name'], $uploadfile)) {
          echo "GOOD";
       } else {
           echo "CAN'T MOVE FILE";
       }
    } else {
       echo "ERROR";
    }

}
else{
    echo "Error In Uploading File";
}
*snip*

Additional Info

  • I am running wordpress
  • Folder is chmod 777
  • Upload forms enabled in php.ini
  • File is smaller than max filesize in both <form> and php.ini
1
error_reporting(E_ALL); ini_set('display_errors', true); This should give you a warning about what went wrong. - deceze
what size is your file? sometimes browser does not permit you that upload a big file. - NrNazifi

1 Answers

0
votes

I tried this and it's working fine:

<?php
//print_r($_FILES);


if($_FILES['chartimage']['error'] == '0'){
    $uploaddir = 'images\\';
    $file = ($_FILES['chartimage']['name']);

    $uploadfile = $uploaddir . $file;

    if(file_exists($_FILES['chartimage']['tmp_name'])){
       move_uploaded_file($_FILES['chartimage']['tmp_name'], $uploadfile);
    } else {
       echo "ERROR";
    }

}
else{
    echo "Error In Uploading File";
}
?>

When you printed out $uploadfile = $uploaddir . $file; echo $uploadfile; with your code, I got: images/C:\WINDOWS\Temp\php76.tmp which is the source of the problem.