Jump to content

[SOLVED] Upload Default Pic


greenday

Recommended Posts

Hi, I am using a form to upload 5 image names to a database, and store the file in a folder. However, if only 4 or less of the five fields are filled out, the databases default image 'no image to display' is overwritten by nothing.

 

Is there any way to write an if field is blank, use default.gif file, else use uploaded file? Here is the code:

 

mysql_query("UPDATE `listtable` SET

`mainpic` = '".$_FILES['pic']['name'][0]."',

`pic2` = '".$_FILES['pic']['name'][1]."',

`pic3` = '".$_FILES['pic']['name'][2]."',

`pic4` = '".$_FILES['pic']['name'][3]."',

`pic5` = '".$_FILES['pic']['name'][4]."',

`pic6` = '".$_FILES['pic']['name'][5]."'

WHERE `placeid` = '$plid'");

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/58493-solved-upload-default-pic/
Share on other sites

something like this perhaps - I only did it on pic 5 and 6 so you can see the diff..

 

$default = 'mypic.png';

mysql_query("UPDATE `listtable` SET
`mainpic` = '".$_FILES['pic']['name'][0]."',
`pic2` = '".$_FILES['pic']['name'][1]."',
`pic3` = '".$_FILES['pic']['name'][2]."',
`pic4` = '".$_FILES['pic']['name'][3]."',
`pic5` = '".( empty($_FILES['pic']['name'][4]) ? $default : $_FILES['pic']['name'][4] )."',
`pic6` = '".( empty($_FILES['pic']['name'][5]) ? $default : $_FILES['pic']['name'][5] )."'
WHERE `placeid` = '$plid'");

On another note, this is extremely insecure - crafted filenames could inject you sql - you really need to clean input and output!

 

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.