Page 1 of 1

upload file

Posted: Mon Jan 04, 2010 3:55 pm
by michaelk46
I have a script that uploads a file and moves it to folder marked csv. When it runs, it says that it moves the file, but the folder it is supposed to be moved into, is always empty...

Code: Select all

 
switch($_FILES['upload']['error'])
            {
                case UPLOAD_ERR_OK:
                    $ext = strtolower(pathinfo($_FILES['upload']['name'], PATHINFO_EXTENSION));
                    switch ($ext)
                        {
                            case 'csv':
                                $destfile='\csv' . basename($_FILES['upload']['name']);
                                $ret = move_uploaded_file($_FILES['upload']['tmp_name'], $destfile);
                                switch ($ret)
                                    {
                                        case false:
                                            echo htmlspecialchars('Unable to move file', ENT_QUOTES, 'utf-8');
                                        break;
                                        default:
                                            echo htmlspecialchars('File moved successfully', ENT_QUOTES, 'utf-8');
                                        break;
                                    }
                            break;
                            default:
                             echo htmlspecialchars('Invalid File Type', ENT_QUOTES, 'utf-8');
                break;
 
and here is the form code

Code: Select all

 
<form enctype="multipart/form-data" action="index.php" method="post">
    <div> 
        <label for="upload">File to Be Uploaded:</label> 
        <input name="upload" type="file" size="45"/>
    </div> 
    <p></p>
    <div>
        <input type="submit" name="file_upload" value="Upload File"/>
    </div>
</form>
 
The folder it is supposed to move the file into is inside the same folder that the file currently resides in.

Help as to why it is not moving the file greatly appreciated

Re: upload file

Posted: Mon Jan 04, 2010 5:19 pm
by cpetercarter
The /csv directory has to be writeable by php. Generally, that means that it should have permissions set to 0777.

Re: upload file

Posted: Mon Jan 04, 2010 5:24 pm
by michaelk46
I have full permissions on the folder

Re: upload file

Posted: Mon Jan 04, 2010 6:02 pm
by SimpleManWeb
Just a quick guess after quickly scanning your code, but could it be because you defined your file as '\csv'. I doubt it will work with a back slash. Try changing that to a forward slash or else just remove it all together if your 'csv' folder is in the same directory that this code is running from. Hope that helps.

Re: upload file

Posted: Mon Jan 04, 2010 6:45 pm
by michaelk46
It didn't work, but I just noticed something....

After the script ran, it showed a plus sign in Dreamweaver next to the folder like something was in there. But when I clicked on the folder, the plus sign disappeared and the folder was empty.

Could it be deleting the file after it copies???

Re: upload file

Posted: Mon Jan 04, 2010 7:04 pm
by michaelk46
Got it fixed...

I found that it was actually moving the file but just not where I wanted it to go... It was moving it to the folder above where the file was instead of the csv folder which was in the same folder as the file to be moved... Weird.

But once I had to put the entire path to the folder in $destfile and not just the part relative to where the file was, it worked like a champ... One problem down... unknown amount to go...

Re: upload file

Posted: Mon Jan 04, 2010 10:16 pm
by SimpleManWeb
Glad to hear that you got it figured out. Thanks for posting the solution.