emediastudios Posted November 6, 2007 Share Posted November 6, 2007 Hi everyone, been working on this code for a while and wish to add a filter that checks for file exist, file size and extensions. Is there an easy way to add it to my code below. <?php require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; //This gets all the other information from the form $headline=$_POST['headline']; $contents=$_POST['contents']; $photo1=($_FILES['photo1']['name']); $photo2=($_FILES['photo2']['name']); $photo3=($_FILES['photo3']['name']); $title1=$_POST['title1']; $title2=$_POST['title2']; $title3=$_POST['title3']; $link=$_POST['link']; //Writes the photo to the server for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = $target . basename( $_FILES[$photo]['name']); if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } } ?> Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/ Share on other sites More sharing options...
Demonic Posted November 6, 2007 Share Posted November 6, 2007 Have you checked the manual? http://us.php.net/filesize http://us.php.net/file_exists Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/#findComment-385383 Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 http://www.w3schools.com/php/php_file_upload.asp Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/#findComment-385385 Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 http://www.w3schools.com/php/php_file_upload.asp Thanks for that. Sorry for being stupid(still learning php) but if my input fieds are named photo1, photo2, and photo3 how would i change the code ubove to suit my form? Thanks for all your help. <?php require_once('../Connections/p2w.php');?> <?php if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } ?> <?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } } else { echo "Invalid file"; }?> <?php if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("../news/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "../news/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/#findComment-385400 Share on other sites More sharing options...
Demonic Posted November 6, 2007 Share Posted November 6, 2007 http://www.w3schools.com/php/php_file_upload.asp Thanks for that. Sorry for being stupid(still learning php) but if my input fieds are named photo1, photo2, and photo3 how would i change the code ubove to suit my form? Thanks for all your help. <?php require_once('../Connections/p2w.php');?> <?php if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } ?> <?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } } else { echo "Invalid file"; }?> <?php if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("../news/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "../news/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> hehe we're all noobs . Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/#findComment-385410 Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 <?php require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; //This gets all the other information from the form $headline=$_POST['headline']; $contents=$_POST['contents']; $photo1=($_FILES['photo1']['name']); $photo2=($_FILES['photo2']['name']); $photo3=($_FILES['photo3']['name']); $title1=$_POST['title1']; $title2=$_POST['title2']; $title3=$_POST['title3']; $link=$_POST['link']; //Writes the photo to the server for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = $target . basename( $_FILES[$photo]['name']); if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") && ($_FILES["file"]["size"] < 20000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } ?> try Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/#findComment-385415 Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 Thanks ever so much. so gratefull for your help. i tried your code and this echoed 3failed to upload ??? Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/#findComment-385418 Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 sorryyyyyy $_FILES["file"]["type"] should be $_FILES[$photo]["type"] Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/#findComment-385427 Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 changed the code to <?php require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; //This gets all the other information from the form $headline=$_POST['headline']; $contents=$_POST['contents']; $photo1=($_FILES['photo1']['name']); $photo2=($_FILES['photo2']['name']); $photo3=($_FILES['photo3']['name']); $title1=$_POST['title1']; $title2=$_POST['title2']; $title3=$_POST['title3']; $link=$_POST['link']; //Writes the photo to the server for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = $target . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 20000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } ?> Get the same echo 3failed to upload We'll get there Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/#findComment-385430 Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 do this for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; echo '<pre>'; print_r($_FILES); echo '</pre>'; to see whats insde the file and paste the result Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/#findComment-385432 Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 i get this Array ( [photo1] => Array ( [name] => Lineup_220907_1024_200792511327.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC93F.tmp [error] => 0 [size] => 213446 ) [photo2] => Array ( [name] => megan_fox_02.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC94F.tmp [error] => 0 [size] => 65963 ) [photo3] => Array ( [name] => megan_fox_05.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC950.tmp [error] => 0 [size] => 73388 ) ) Array ( [photo1] => Array ( [name] => Lineup_220907_1024_200792511327.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC93F.tmp [error] => 0 [size] => 213446 ) [photo2] => Array ( [name] => megan_fox_02.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC94F.tmp [error] => 0 [size] => 65963 ) [photo3] => Array ( [name] => megan_fox_05.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC950.tmp [error] => 0 [size] => 73388 ) ) Array ( [photo1] => Array ( [name] => Lineup_220907_1024_200792511327.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC93F.tmp [error] => 0 [size] => 213446 ) [photo2] => Array ( [name] => megan_fox_02.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC94F.tmp [error] => 0 [size] => 65963 ) [photo3] => Array ( [name] => megan_fox_05.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC950.tmp [error] => 0 [size] => 73388 ) ) Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/#findComment-385435 Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 see this part of your array => 213446 $_FILES[$photo]["size"] < 20000)<-- part of your condition see whats wrong? try to remove that part on your if and or if that aont work you have to set you max upload size Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/#findComment-385437 Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 ok for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = $target . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg")){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } ?> Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/#findComment-385442 Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 all the file upload and record is inserted, just one strange thing happening now. file one named in folder 11.jpg, file two named 11.jpg12.jpg, file 3 renamed 11.jpg12.jpg19.jpg ??? Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/#findComment-385446 Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 post your recent code? Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/#findComment-385452 Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 <?php require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; //This gets all the other information from the form $headline=$_POST['headline']; $contents=$_POST['contents']; $photo1=($_FILES['photo1']['name']); $photo2=($_FILES['photo2']['name']); $photo3=($_FILES['photo3']['name']); $title1=$_POST['title1']; $title2=$_POST['title2']; $title3=$_POST['title3']; $link=$_POST['link']; //Writes the photo to the server for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = $target . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg")){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } ?> Link to comment https://forums.phpfreaks.com/topic/76146-image-filter/#findComment-385454 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.