Upload Problem

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
WhoCares357
Forum Newbie
Posts: 3
Joined: Tue Apr 01, 2008 10:24 am

Upload Problem

Post by WhoCares357 »

Hey. I've been learning php for the past few days, but I've run into a few problems. I'm using a free host (zxq).

I'm trying to upload file to my server.
The code for the upload page is:

Code: Select all

<html>
    <head>
        <title>Files</title>
    </head>
    <body>
 
        Want to upload some files?<br />
        Max File Size: 500mb<br /><br />
 
        <form action="upload_file.php" method="POST"
         enctype="multipart/form-data">
            <label for="file">Filename: </label>
            <input type="file" name="file" id="file" /><br />
            <input type="submit" name="submit" value="Submit" />
        </form>
 
    </body>
</html>
upload_file.php is this:

Code: Select all

<?php
 
if ($_FILES["file"]["size"] > (500*1024*1024))
    echo "File is too large. The limit is 500 mb.";
    
elseif ($_FILES["file"]["error"] > 0)
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
        
else    
    {
        //storing temporarily & stats
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        //echo "Stored in: " . $_FILES["file"]["tmp_name"];
        
        //moving to permanent spot
        if (file_exists("upload/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " already exists. ";
        }
        else
        {
            move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'],
            "/uploads/{$_FILES["file"] ["name"]}");
            echo "Stored in: " . "../uploads/" . $_FILES["file"]["name"];
        }
    }
 
?>
I manually created the folder uploads so it does exist.

When I try to upload a file, it tells me that it's uploaded (says it's name, size, location, etc.) but it doesn't actually appear on the server. Am I doing something wrong?

Thanks for any help.

Edit: I've rewritten the code for upload_file.php a bit. I figured out the problem occurs while moving the file:

Code: Select all

<?php
 
if ($_FILES["file"]["size"] > (500*1024*1024))
    echo "File is too large. The limit is 500 mb.";
    
elseif ($_FILES["file"]["error"] > 0)
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
        
else    
    {
        //storing temporarily & stats
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        //echo "Stored in: " . $_FILES["file"]["tmp_name"];
        
        //moving to permanent spot
        if (file_exists("upload/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " already exists. ";
        }
        else
        {
            if (move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'],
            "/uploads/{$_FILES["file"] ["name"]}"))
                echo "Stored in: " . "../uploads/" . $_FILES["file"]["name"];
            else
                echo "There was some error while moving the file.";
        }
    }
 
?>
Last edited by WhoCares357 on Tue Apr 01, 2008 10:57 am, edited 1 time in total.
AMCH
Forum Commoner
Posts: 31
Joined: Sun Mar 30, 2008 4:39 pm

Re: Upload Problem

Post by AMCH »

Code: Select all

<?php
 
if ($_FILES["file"]["size"] > (500*1024*1024))
echo "File is too large. The limit is 500 mb.";
 
elseif ($_FILES["file"]["error"] > 0)
echo "Error: " . $_FILES["file"]["error"] . "<br />";
       
else
How come no curly brackets?

Code: Select all

<?php
 
if ($_FILES["file"]["size"] > (500*1024*1024))
{
echo "File is too large. The limit is 500 mb.";
}
elseif ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}    
else
:crazy:

Do you not get syntax errors?

AMCH
WhoCares357
Forum Newbie
Posts: 3
Joined: Tue Apr 01, 2008 10:24 am

Re: Upload Problem

Post by WhoCares357 »

I'm not using braces there because it's only doing one thing after the if statement. You can do that.

Also, I've rewritten the code for upload_file.php a bit. I figured out the problem occurs while moving the file:

Code: Select all

<?php
 
if ($_FILES["file"]["size"] > (500*1024*1024))
    echo "File is too large. The limit is 500 mb.";
    
elseif ($_FILES["file"]["error"] > 0)
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
        
else    
    {
        //storing temporarily & stats
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        //echo "Stored in: " . $_FILES["file"]["tmp_name"];
        
        //moving to permanent spot
        if (file_exists("upload/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " already exists. ";
        }
        else
        {
            if (move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'],
            "/uploads/{$_FILES["file"] ["name"]}"))
                echo "Stored in: " . "../uploads/" . $_FILES["file"]["name"];
            else
                echo "There was some error while moving the file.";
        }
    }
 
?>
WhoCares357
Forum Newbie
Posts: 3
Joined: Tue Apr 01, 2008 10:24 am

Re: Upload Problem

Post by WhoCares357 »

Any help? (Is there a bump button?)
thaynejo
Forum Newbie
Posts: 14
Joined: Tue Apr 01, 2008 9:06 am

Re: Upload Problem

Post by thaynejo »

I am not seeing any code in there that actually uploads the file. If you have a copy function already (for reference see: http://us3.php.net/manual/en/function.copy.php), post the code for it, otherwise, that is your problem.
Post Reply