shadiadiph Posted December 21, 2008 Share Posted December 21, 2008 this script is great but how would I make it so the form can only upload jpegs and gifs? At the moment it uploads any file type? html form <? ?> <html> <title></title> <head> </head> <body> <form method="post" action="upload.php" enctype="multipart/form-data"> Description:<br> <input type="text" name="form_description" size="40"> <input type="hidden" name="MAX_FILE_SIZE" value="250000"> <br>File to upload:<br> <input type="file" name="form_data" size="40"> <p><input type="submit" name="submit" value="submit"> </form> </body> </html> php page <?php include("global/connection.php"); include("global/mail.class.php"); error_reporting(7); $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); $result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype) ". "VALUES ('$form_description1','$data','$form_data_name','$form_data_size','$form_data_type')"); $id= mysql_insert_id(); print "<p>File ID: <b>$id</b><br>"; print "<p>File Name: <b>$form_data_name</b><br>"; print "<p>File Size: <b>$form_data_size</b><br>"; print "<p>File Type: <b>$form_data_type</b><p>"; print "To upload another file <a href=http://wwwwebsite.com/filetest.php> Click Here</a>"; ?> Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/ Share on other sites More sharing options...
carrotcake1029 Posted December 21, 2008 Share Posted December 21, 2008 How would you like to check? Do you want to check the file extension of the potential image or do you want to read some of the image header to check? Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/#findComment-720724 Share on other sites More sharing options...
DeanWhitehouse Posted December 21, 2008 Share Posted December 21, 2008 Have a read through http://www.plus2net.com/php_tutorial/php_file_upload.php Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/#findComment-720730 Share on other sites More sharing options...
shadiadiph Posted December 21, 2008 Author Share Posted December 21, 2008 mm i looked at that page funnily enough yesterday and couldn't get it to work just tried again with if (!($userfile_type =="image/pjpeg" OR $userfile_type=="image/gif")){$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>"; $file_upload="false";} changed userfile_type to form_data_type didn't work I just want to allow the user to upload gifs and jpegs only I just tried the above and was able to successfully upload a word document? Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/#findComment-720738 Share on other sites More sharing options...
DeanWhitehouse Posted December 21, 2008 Share Posted December 21, 2008 Maybe this one will explain it in a way that is clearer to you http://www.w3schools.com/PHP/php_file_upload.asp Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/#findComment-720741 Share on other sites More sharing options...
shadiadiph Posted December 21, 2008 Author Share Posted December 21, 2008 thanks blade works fine with this. <?php include("global/connection.php"); include("global/mail.class.php"); error_reporting(7); if ((($_FILES["form_data"]["type"] == "image/gif") || ($_FILES["form_data"]["type"] == "image/jpeg") || ($_FILES["form_data"]["type"] == "image/pjpeg")) && ($_FILES["form_data"]["size"] < 250000)) { if ($_FILES["form_data"]["error"] > 0) { echo "Error: " . $_FILES["form_data"]["error"] . "<br />"; } else { $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); $result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype) ". "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')"); $id= mysql_insert_id(); print "<p>File ID: <b>$id</b><br>"; print "<p>File Name: <b>$form_data_name</b><br>"; print "<p>File Size: <b>$form_data_size</b><br>"; print "<p>File Type: <b>$form_data_type</b><p>"; print "To upload another file <a href=http://www.hmtcompany.com/filetest.php> Click Here</a>"; } } else { echo "You can only upload JPEG or GIF images that are 250KB or less."; } ?> thanks again now i have to learn about the image library and resizing Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/#findComment-720749 Share on other sites More sharing options...
lokie538 Posted December 22, 2008 Share Posted December 22, 2008 You might be interested in this, just made it a few days ago! <?php // This is the temporary file created by PHP $uploadedfile = $_FILES['uploadfile']['tmp_name']; $uploadedfiletype = $_FILES['uploadfile']['type']; if (!($uploadedfiletype =="image/pjpeg" OR $uploadedfiletype =="image/jpeg" OR $uploadedfiletype =="image/jpg")){ header ( "Location: uploadindex.php?stat=jpg" ); }; // Create an Image from it so we can do the resize $src = imagecreatefromjpeg($uploadedfile); // Capture the original size of the uploaded image list($width,$height)=getimagesize($uploadedfile); $height_orig = $height; $width_orig = $width; // For our purposes, I have resized the image to be // 600 pixels wide, and maintain the original aspect // ratio. This prevents the image from being "stretched" // or "squashed". If you prefer some max width other than // 600, simply change the $width_max variable // maximum height and width $width_max = "600"; $height_max = "800"; // initialize reduced dimensions $heightr = $height_orig; $widthr = $width_orig; // if height exceeds max, scale down dimensions if ($height_max < $heightr) { $heightr = $height_max; $widthr = ($height_max / $height_orig) * $width_orig; } // if width still exceeds max, further scale down dimensions if ($width_max < $widthr) { $heightr = ($width_max / $widthr) * $heightr; $widthr = $width_max; } $tmp = imagecreatetruecolor($widthr, $heightr); // this line actually does the image resizing, copying from the original // image into the $tmp image imagecopyresampled($tmp, $src, 0, 0, 0, 0, $widthr, $heightr, $width_orig, $height_orig); // now write the resized image to disk. I have assumed that you want the // resized, uploaded image file to reside in the ./images subdirectory. $filename = "images". $newname . ".jpg"; imagejpeg($tmp,$filename,100); imagedestroy($src); imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request // has completed. echo $width; echo "<br>"; echo $height; echo "<br>"; $imagesize= "../Gallery/Large/". $newname . ".jpg"; list($width2,$height2)=getimagesize($imagesize); echo $width2; echo $height2; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //watermark - passed onto another php file for a watermark to be placed on /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// header ( "Location: watermark.php?picid=" . $newname ); ?> Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/#findComment-721182 Share on other sites More sharing options...
shadiadiph Posted December 22, 2008 Author Share Posted December 22, 2008 mm thanks but my problem at the moment is i am only writing images to the sql database for easy indexing. thanks anyway Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/#findComment-721246 Share on other sites More sharing options...
lokie538 Posted December 22, 2008 Share Posted December 22, 2008 Nah that wasnt to help with your current problem, that was a script for image resizing which you said you were going to look at next. You might need to resize the image before adding it too the database. Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/#findComment-721280 Share on other sites More sharing options...
shadiadiph Posted December 22, 2008 Author Share Posted December 22, 2008 thanks anyway everything i have tried isn't working so i am going to clear the page and start fresh if it hasn't worked in a hundred times start from scratch Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/#findComment-721307 Share on other sites More sharing options...
shadiadiph Posted December 22, 2008 Author Share Posted December 22, 2008 hi lokie i just tried your script it didn't save the image to the images folder?? i made sure it was a jpg image Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/#findComment-721708 Share on other sites More sharing options...
shadiadiph Posted December 22, 2008 Author Share Posted December 22, 2008 getting this error 1024 768 Warning: getimagesize(../Gallery/Large/.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /home/hmcompa/public_html/user/submit.php on line 123 > Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/#findComment-721715 Share on other sites More sharing options...
shadiadiph Posted December 23, 2008 Author Share Posted December 23, 2008 mm kind of works but uploads the file as name image.jpg to the same directory as the php file doesn't save in the folder images i am trying to figure how to make it save to my database table uploads? Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/#findComment-722107 Share on other sites More sharing options...
shadiadiph Posted December 23, 2008 Author Share Posted December 23, 2008 how can i write this which is currently saving to the diercory the php file is in $filename = "images". $newname . ".jpg"; imagejpeg($tmp,$filename,100); into $result=MYSQL_QUERY("INSERT INTO uploads ????? Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/#findComment-722115 Share on other sites More sharing options...
lokie538 Posted December 23, 2008 Share Posted December 23, 2008 hmm not sure? make a new thread and others will help Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/#findComment-722288 Share on other sites More sharing options...
shadiadiph Posted December 23, 2008 Author Share Posted December 23, 2008 i already got it to upload to the database just having a problem viewing right now thanxs Link to comment https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/#findComment-722293 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.