Jump to content

Extending the functionality of my upload script


Fallen_angel

Recommended Posts

Hi ,  I need a bit of assistance with an upload script I am doing , 
it is all working fine so far however I would like to extend the functionality of the script.

this is what I have so far
[code]function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}

//This applies the function to our file
$ext = findexts ($_FILES['uploaded']['name']) ;

    //This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
    $ran = rand () ;

    //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
    $ran2 = $ran.".";

    //This assigns the subdirectory you want to save into... make sure it exists!
    $target = "../uploads/";

    //This combines the directory, the random file name, and the extension
    $target2 = $target . $ran2.$ext;

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target2))
{
echo "The first file has been uploaded as $ran2$ext</br>";
}
else
{
echo "Sorry, there was a problem uploading your  file.";
}[/code]

The code above  , acepts an image that is given by a form and first checks its size ( done on the form), then it  renames it to a random filename , and moves the file to the uploads folder , ( making a field entry in my database with the file name )

I would like to add the following functionality to the script if possible please , 1st thing I want to do is generate a thumbnail file on the fly , so that when the file is uploaded both a copy of the thumbnail image and also of the fullsized image are uploaded to my upload folder  this will mean I can place them in the apropriate spots of my webapp without having to set height and width atributes inside of an image tag ( which is what i am currently doing ) ,  Seccondly I would like to check the file type of the files beign uploaded so that I can refuse the uploading of certain filetypes .

Thankyou every so much to anyone that can help me

For the first part and this is purely a guess on my part because I have seen this a few times elsewhere is that you have to use the GD-Lib (its a library) on your server. Please dont ask, I am not 100% sure on that point, I have just seen it mentioned a lot with thumbnails but its a good place to start.

The second part is a lot easier to do basically if you are renaming the file you must be using some sort of string function to replace all the charactors before the "."

If so you can use a simple explode statement on the "." to split your filename into filename and extension then use a conditional statement like IF or SWITCH (preferably switch) to filter through each extension type you want to test for.

Ok I have this so far

the file doesn't throw out any errors but i know i have somethign wrong because the file is not being uploaded to the thumbnail directory

[code]function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}

//This applies the function to our file
$ext = findexts ($_FILES['uploaded']['name']) ;

    //This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
    $ran = rand () ;

    //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
    $ran2 = $ran.".";

    //This assigns the subdirectory you want to save into... make sure it exists!
    $target = "../uploads/";

    //This combines the directory, the random file name, and the extension
    $target2 = $target . $ran2.$ext;
    $imagefile = $ran2.$ext;
  $thumbtarget=  "../uploads/thumbs/";

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target2))
{
echo "The first file has been uploaded as $ran2$ext</br>";
}
else
{
echo "Sorry, there was a problem uploading your first file.";
}

function generate_thumbnail($imagefile, $thumb_max_dimension = 200, $quality = 90, $thumbtarget, $thumb_prefix = "thumb_")
{
  /* v1.0.1 (by acdx) */
  $filename_arr = explode(".", basename($imagefile));
  $filetype = $filename_arr[1];

  if($filetype == "jpg")
    $filetype = "jpeg";

  if($filetype != "jpeg" && $filetype != "gif" && $filetype != "png")
    return false;

  $original_size = getimagesize($imagefile);

  eval("\$image = imagecreatefrom".$filetype."(\$imagefile);");

  if($original_size[0] > $original_size[1])
  {
    if($original_size[0] > $thumb_max_dimension)
      $thumb_width = $thumb_max_dimension;
    else
      $thumb_width = $original_size[0];
    $thumb_height = $original_size[1]*($thumb_width/$original_size[0]);
  }
  else
  {
    if($original_size[1] > $thumb_max_dimension)
      $thumb_height = $thumb_max_dimension;
    else
      $thumb_height = $original_size[1];
    $thumb_width = $original_size[0]*($thumb_height/$original_size[1]);
  }

  $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
  imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $original_size[0], $original_size[1]);
  imagejpeg($thumb, $thumbtarget.$thumb_prefix.basename($imagefile).".jpg", $quality);
  imagedestroy($thumb);
  imagedestroy($image);
}[/code]

any ideas as to what i have wrong ?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.