PHP Uploader (Functions)

Soldato
Joined
1 Dec 2004
Posts
23,079
Location
S.Wales
I want to expand my basic file uploader. Here is the code:


Code:
	<?php

//Variables
$_FILES["uploaded_file"]["name"];
$_FILES["uploaded_file"]["type"];
$_FILES["uploaded_file"]["size"];
$_FILES["uploaded_file"]["tmp_name"];
$_FILES["uploaded_file"]["error"];


//Check that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  
  //Check if the file is JPEG image and it's size is less than 300Kb
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
    ($_FILES["uploaded_file"]["size"] < 1200000)) {
    
	//Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/phpupload/uploaddir/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
           echo "It's done! The file has been saved as: ".$newname;
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
      }
  } else {
     echo "Error: Only .jpg images under 300Kb are accepted for upload";
  }
} else {
 echo "Error: No file uploaded";
}
?>

and i have a basic form which consists of a text field, a browse button, and an upload button which points to the above PHP.

At the moment i can only upload 1 file at a time of the one type (jpg).

I have some requirements below:

  1. The ability to upload multiple files in one session (maybe 5 at a time)
  2. If a filetype of the same name is uploaded, i want it to amend the name instead of refusing to upload it
  3. The ability to upload files of multiple types (jpg, wmv, avi, pdf, doc)
  4. A seperate page that will display a gallery (small thumbnails of the images only that have been uploaded.

How easy would it be to do the above?
 
Most of what you lot have suggested i just cant get my head around, not unless i start this script from scratch, i cant encorporate it into my existing code.

I have worked out on my own how do upload different extensions, even if it is abit long winded!


Code:
$filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg")  || ($ext == "txt") && ($_FILES["uploaded_file"]["type"] == "text/plain") || ($ext == "gif") && ($_FILES["uploaded_file"]["type"] == "image/gif") && ($_FILES["uploaded_file"]["size"] < 307200)) {
 
Created a new script from scratch, which is below:

Code:
<?php

//Variable Declarations
$_FILES["uploaded_file"]["name"];
$_FILES["uploaded_file"]["type"];
$_FILES["uploaded_file"]["size"];
$_FILES["uploaded_file"]["tmp_name"];
$_FILES["uploaded_file"]["error"];

$target = "phpupload/uploaddir/"; //Upload Directory
$target = $target . basename( $_FILES['uploaded_file']['name']) ;  //Target file details



$ok=1; //Error Generator (1=No Error, 0= Error)



//This is our size condition, Files must be under 300kb
if ($uploaded_size > 307200)
{
echo "Files need to be under 300kb <br>";
$ok=0;
}


//This is our limit file type condition, supported types include 
if ($uploaded_type !="image/jpeg")
{
echo "No supported files<br>";
$ok=0;
} 


//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}

//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploaded_file']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
} 

?>

problem is this part

Code:
/This is our limit file type condition, supported types include 
if ($uploaded_type !="image/jpeg")
{
echo "No supported files<br>";
$ok=0;
}

From that snippet, what you would expect it to upload all files with the "image/jpeg" mime type, if the uploaded type is not equal to "image/jpeg" it will echo the error "No supported files" and set the error count to 0 (0 means that there are errors)

but that doest work, iv tried lots of combos which dont seem to work, i can only get all files to upload, or none.
 
just underneath it, outside if the if(){} statement, put:

echo "The Upload Type was $uploaded_type <br>";

Then you'll see what's actually in your $uploaded_type variable.


It doesnt output nothing, nothing is being stored in the variable, hence it aint working.

Ill have ago at that switch array one, its far too complicated for my level of coding, think i might just give it a miss :(
 
It doesnt do anything, when i click submit i just get a blank page and nothing its uploaded/outputted
 
psyr33n said:
Jesus, ask for help then dismiss it... seriously, just ****ing try it and experiment.
im not being funny mate but chill out.its alrite for some people who can read this stuff and get it.i appriciate everyones help on here and as i said im going to have ago when i get the chance. If the web was full of decent tutorials i wouldnt be spending most of my day on google wasting my time.but for programming its close to useless to me.
 
find me a tutorial of a working,decent,secure upload script and i will appoligise.every one i have tried isnt fit for use on a live website.
 
marc2003 said:
at first i thought this might be because i only tested on a non-apache windows server. :p but i've just tested on a ubuntu/apache box and it works fine. so the problem is with you i'm afraid..... :)

edit: i've posted a more functional script here

I did get it working in the end, well the file types anyway, i just need to work on the rest of the switch case method including the rest of the restrictions, to test that its worked iv just echo'ed $message at the end and check the file has been uploaded.
 
marc2003 said:
take a look at the link you just quoted. i posted the full version of the script i did. does the whole thumbnail creation, more image types, displays all the uploaded images, allows you to delete etc....


Ill have a look at it, thanks for posting it, ill probably get it working, and try and analyse the code, so i can see how it works, i wanted to try and make one on my own so i learn how to do it, not making a good job of it though :rolleyes:
 
Back
Top Bottom