0

I'm working on a project using codeigniter 3.0.6 and I need to upload image in some modules. the problem is that move_uploaded_file returns error code 0 but the file is nowhere to be found. I'm currently running this code on my localhost. if only it returns an error code then I can do something.. can anyone help me with this?

I've read Move_uploaded_file() function is not working too and my code below is based on one of the supposedly "working example" answer.. but still not working (error code 0 but file not found in directory). the mkdir($uploaddir, 0777, true); part doesn't seem to help.

this is my view file (display.php) :

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <form enctype="multipart/form-data" action="submit" method="POST">
            <input type="hidden" name="MAX_FILE_SIZE" value="51200000" />
            Send this file: <input name="userfile" type="file" />
            <input type="submit" value="Send File" />
        </form>     
    </body>
</html>

and this is my controller file (test.php) :

function display()
{
    $this->load->view('display');
}

function submit() {
    $uploaddir = '/assets/img/guide/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

    if (!file_exists($uploaddir)) {
        mkdir($uploaddir, 0777, true);
    } 

    echo "<p>";
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "File is valid, and was successfully uploaded.\n";
    } else {
        echo "Upload failed";
    }
    echo "</p>";

    echo '<pre>';
    echo 'Error code:';
    print_r($_FILES['userfile']['error']);
    print "</pre>";     

    echo '<pre>';
    echo 'Here is some more debugging info:';
    print_r($_FILES);
    print "</pre>";     

    echo $this->load->view('display', $this->data);
}

and this is the result after I submit the form:

File is valid, and was successfully uploaded.

Error code:0

Here is some more debugging info:Array ( [userfile] => Array ( [name] => Hydrangeas.jpg [type] => image/jpeg [tmp_name] => C:\xampp\tmp\php46FD.tmp [error] => 0 [size] => 595284 )

)

Community
  • 1
  • 1
dapidmini
  • 1,490
  • 2
  • 23
  • 46
  • 1
    Codeigniter has a nice upload library to use. Why dont you use it? – M Reza Saberi Jun 27 '16 at 10:46
  • 1
    Don't get confused: that 0 comes from the upload preprocessor (`$_FILES['userfile']['error']`); `move_uploaded_files()` does not return any code but just a boolean (which appears to be `true` in your case). If there's an error in that phase, it'll show up as PHP warning. – Álvaro González Jun 27 '16 at 10:46
  • Use do_upload() in CodeIgniter, – Fil Jun 27 '16 at 10:49
  • I used to code in flat php so I guess old habits die hard.. and also, I thought native php function such as move_uploaded_file is faster? I'll try to use do_upload to avoid wasting time, but still curious about this :P @EvisBregu the assets folder is on my root directory – dapidmini Jun 27 '16 at 11:01
  • Is your upload folder correct? It seems that you are using an absolute path. Where is your assets folder physically, inside your web root folder or on your operating system root? – Evis Bregu Jun 27 '16 at 11:02

1 Answers1

0

it turns out the files WERE uploaded but instead of going to my project's base folder (D:/myproject/dev/), the files were uploaded to the harddrive folder (D:/) so i guess the mistery's solved. I just need to figure out how to direct the target folder for upload.. maybe I'll put it in another question post.

thank you all

dapidmini
  • 1,490
  • 2
  • 23
  • 46