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!
i facin some prob in my college final project, do u guys hav similiar PHP code regarding pic/image uploading and store in a folder and the "PhotoID" store in MYSQL.
For example, when i login as "ABC" and i upload a pic. THen the uploaded PIC name will automatically store same as my login name. such as FLOWER.JPG => ABC.JPG
<?php
if ($upload)
{
if (checks on file type or sizes w/e)
{
echo "ERROR";
exit();
}
else
{
copy($file,"root/to/place/to/store/it");
rename($file, $username);
}
?>
something like that maybe ? i'm not sure...i'm a newbie to PHP still. I'm just doing what i can to help ppl out
<?php
//..
$image_info = @getimagesize($_FILES['userfile']['tmp_name'];
if (!is_int($image_info[2]) || $image_info[2] < 0 || $image_info[2] > 16) {
//the file is not a picture, display some error message
}
else {
//the file is a picture, so move it
move_uploaded_file($_FILES['userfile']['tmp_name'], "/home/path/to/your/folder/".$_FILES['userfile']['name']);
echo "it's all good";
//do your mysql
}
?>
As you can see, I used PHP's internal [php_man]getimagesize[/php_man] function, which returns (among other things) the type of image that it is. If something other than an image has been uploaded, getimagesize() will issue a warning (which is why I put an error supressant (@) in front of it), and $image_info[2] will not be an integer (hence the is_int() in the if statement). Look in the php manual for more details on getimagesize(). The reason I don't like using mime types (your way) is because a mime type can be easily spoofed, while this does a more in-depth check.
thank you for you all's comments...
i can upload the picture file to the folder..
izit possible to change the picture file name according the the login name?
for example, when i login using ABC, the original picture pic.jpg change to ABC.jpg
thanks...