PHP: Uploading files to server

software development

#1

Right now I have a script that uploads and stores images in a database. I have been advised that uploading to my server would save resources and increase the speed in which the images are displayed. Is there an easy way to change what I have now to save to my server? I hate to paste a lot of code in a forum, but here I go. This is how I’m uploading my images to a database:

[code]if($_FILES[‘userfile1’][‘size’] > 0){
$limit_size = imageLimit();//file size limit
$fileName = $_FILES[‘userfile1’][‘name’];
$tmpName = $_FILES[‘userfile1’][‘tmp_name’];
$fileSize = $_FILES[‘userfile1’][‘size’];
$fileType = $_FILES[‘userfile1’][‘type’];

$fp = fopen($tmpName, ‘r’);
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc()){ $fileName = addslashes($fileName);}
switch($fileType) {
case ‘image/pjpeg’:
$extension=“jpeg”;
break;
case ‘image/gif’:
$extension=“gif”;
break;
case ‘image/x-png’:
$extension=“png”;
break;
case ‘image/bmp’:
$extension=“bmp”;
break;
default:
echo “You selected an invalid image type. Please go back and try again.”;
}
$used=usedSpace();//amount of spaced used by a user
$added=$fileSize+$used;//used space plus current file
$allowed=diskSpace();//total space available to a user
if($fileSize > $limit_size)
echo “File $fileName’s size is over the limit”;
else if($added>$allowed)
echo “You do not have enough disk space to upload this image”;
else if(isset($extension)){
$image1_id=getKey();
$query = "INSERT INTO image (image_id,image_name,image_size,image_type,image) ".
“VALUES (’$image1_id’,’$bill_id’,’$userKey’,’$fileName’,’$fileSize’,’$fileType’,’$content’)”;
mysql_query($query) or die(mysql_error());
}
else{ echo “failure?”; }
}
[/code]

Thanks for the help guys!


#2

Without going into a detailed analysis (I’m busy with Christmas prep!), you should be able to take advantage of the move_uploaded_file() function. There are some good file upload examples on the PHP site.

You’re best bet is probably the Uber-Uploader though.

si-blog
Max discount on any plan with promocode SCJESSEYTOTAL


#3

You can refer this for example also
Hanlding file uploads

For the above example, you can change

$uploaddir = ‘/var/www/uploads/’;

to

$uploaddir = getcwd().’/’; // refer to current location

$50 off and 3 free domains with code: [color=#CC0000]DH3[/color] Sign Up NOW or More Codes Here


#4

If you’re just uploading images, this might interest you.

I’ve been using it for a good 3 years now and it works a treat. It enables you to resize images either absolutely, or proportionally, and gives you some other nifty functionality using GDI (GDI2 i think).

I’ve used this and also combined it with a bit of extra code to overlay a watermark on the images.

I’ve done this in a few places, one of which is the “Paintings” section of http://www.marcoart.co.uk. The thumbs and the main images were all created with that script, and the “SAMPLE” overlay text with my own code (using imagecopymerge).

As usual, apologies if i’m ranting OT - just thinking it might be a useful suggestion if you need to do some image manipulation at upload time :slight_smile:

Cheers,
Karl

web design, development & seo by DigitalVibe


#5

Great information everyone! I actually got this working with this tutorial. OT rants are welcome. Thats how I find out about cool things I can do with PHP.