upload file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

upload file

Post 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
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: upload file

Post by cpetercarter »

The /csv directory has to be writeable by php. Generally, that means that it should have permissions set to 0777.
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: upload file

Post by michaelk46 »

I have full permissions on the folder
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: upload file

Post 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.
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: upload file

Post 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???
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: upload file

Post 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...
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: upload file

Post by SimpleManWeb »

Glad to hear that you got it figured out. Thanks for posting the solution.
Post Reply