uploader

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
Vietboy
Forum Commoner
Posts: 41
Joined: Mon Dec 01, 2003 10:59 pm

uploader

Post by Vietboy »

why I can't let people upload files to my web?

I have PHP, mySQL support server running.

I set the upload folder chmod to 777
and I upload php file up by ascii mode & binary mode too. no works.. just get no file selected on all php scripts form hotscripts..
but I have browse and select a image file.. why??
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

What errors are you getting? Could you post the code that is giving you trouble?
Vietboy
Forum Commoner
Posts: 41
Joined: Mon Dec 01, 2003 10:59 pm

Post by Vietboy »

upload.php code

Code: Select all

<? 
        if ($file == "none") { 
            print "You must specify a file to upload"; 
        } 
        else { 
        copy($file, "/dw05/d55/vietboy/tmp/$file_name"); 
        unlink($file); 
        } 
    ?>
upload.html code

Code: Select all

&lt;form action="upload.php" method="post" ENCTYPE="multipart/form-data"&gt; 
   &lt;input type="file" size=40 name="file"&gt;&lt;br&gt; 
   &lt;input type="hidden" name="MAX_FILE_SIZE" value="100000"&gt; 
   &lt;input type="submit" value="upload"&gt; 
   &lt;/form&gt;
?>

When I try to upload a filename.txt
it goes to a blank white page.. nothing up.. and when I try my tmp folder where It upload. no file in there

i have chmod 777 tmp folder
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

An important note, that form is very insecure. Anyone who knows PHP could upload a .PHP file to your server and execute it. I would advise that add some security to the form.

Right now this form is setup to check if the file is an image or not (either a .GIF or .JPG). If you want other files to be uploaded then you need to change the MIME types. Here is a list of known MIME types: http://simplythebest.net/info/apache_mime.html

Here is how I would do it:

Code: Select all

<?php
        $uploaddir = $_SERVER['DOCUMENT_ROOT'] . "/path/to/upload/folder/";  //replace the $uploaddir to wherever you want the files to go
        $filename = $_FILES['file']['name'];  // get the filename
        $filetype = $_FILES['file']['type'];  // get the file MIME type
        $uploadfile = $uploaddir . $filename;  //this is full path to where the file will go
        if (empty($filename)) {  //make sure the user selected a file
            die ("You must specify a file to upload!");
        }
        if ($filetype == "image/jpeg" || $filetype == "image/jpg" || $filetype == "image/gif")  {  //make sure file is .jpg or .gif
          if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) { // move the file
          echo "File is valid, and was successfully uploaded. ";
          } 
          else {
          echo "File upload failed";
          }
        }
        else  {
        die ("Please only upload image files.  Your file type was $filetype");
        }
?>
Vietboy
Forum Commoner
Posts: 41
Joined: Mon Dec 01, 2003 10:59 pm

Post by Vietboy »

i use that code and set up my own upload folder

when i try upload a filename.jpg

it reads this "Please only upload image files. Your file type was image/pjpeg"

and jpg should work.. but it doesn't work
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

May be the file isn't a JPEG? Or may be it is a jpeg, but a different type, like jpeg2000 or... i forget the variants, and it's just named .jpg

Did you try a few diff files?
Vietboy
Forum Commoner
Posts: 41
Joined: Mon Dec 01, 2003 10:59 pm

Post by Vietboy »

when i try upload a .gif file

it said "File upload failed "
Post Reply