sungpeng Posted February 27, 2009 Share Posted February 27, 2009 if($imagefile) { $file_upload_err=0; $max_file_size="10000000"; // file size in bytes $max_file_size_kb="500"; $file_upload_err_msg=""; if($_FILES['imagefile']['size']>$max_file_size) { $file_upload_err_msg.="<font color='red'><strong>Image is bigger than the allowed file upload size ($max_file_size_kb kb).</strong></font><br>"; $file_upload_err=1; } if($imagefile && ($_FILES['imagefile']['type']!="image/jpeg" && $_FILES['imagefile']['type']!="image/pjpeg")) { $type=$_FILES['imagefile']['type']; $file_upload_err_msg.="<font color='red'><strong>Image is of a wrong file type.<br>Please upload only JPG images.</strong></font><br>"; $file_upload_err=1; } Good morning, I have the above code. If I would like more imagefile to go through the "if" function. How do I code? like say I have imagefile, imagefile1,imagefile2, imagefile3 so on.. Quote Link to comment Share on other sites More sharing options...
RussellReal Posted February 27, 2009 Share Posted February 27, 2009 for ($i = 0; $e = ${'imagefile'.$i}; $i++) { //use $e instead of $imagefile and use the same code here } Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 27, 2009 Share Posted February 27, 2009 here is a working example that works need to modify it for images though. all files are saved to folder called data which you need to create or change data to thedir you want to save to. foreach ($_FILES["imagefile"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $fileatt = $_FILES['imagefile']['tmp_name'][$key]; $fileatt_type = $_FILES['imagefile']['type'][$key]; $fileatt_name = $_FILES['imagefile']['name'][$key]; move_uploaded_file($fileatt, "data/$fileatt_name"); } } Quote Link to comment Share on other sites More sharing options...
sungpeng Posted February 27, 2009 Author Share Posted February 27, 2009 hi Real, Can you insert the "for" loop into my code. I really don't know how to insert into it. thank Quote Link to comment Share on other sites More sharing options...
sungpeng Posted February 27, 2009 Author Share Posted February 27, 2009 Sorry got it.. The "for" loop is infinity, if my images up to imagefile12 only then how should it be? Quote Link to comment Share on other sites More sharing options...
sungpeng Posted February 27, 2009 Author Share Posted February 27, 2009 for ($i = 0; $eimages = ${'imagefile'.$i}; $i++) { if($eimages) { $file_upload_err=0; $max_file_size="10000000"; // file size in bytes $max_file_size_kb="500"; $file_upload_err_msg=""; if($_FILES['imagefile'.$i]['size']>$max_file_size) { $file_upload_err_msg.="<font color='red'><strong>Image is bigger than the allowed file upload size ($max_file_size_kb kb).</strong></font><br>"; $file_upload_err=1; } if($eimages && ($_FILES['imagefile'.$i]['type']!="image/jpeg" && $_FILES['imagefile'.$i]['type']!="image/pjpeg")) { $type=$_FILES['imagefile'.$i]['type']; $file_upload_err_msg.="<font color='red'><strong>Image is of a wrong file type.<br>Please upload only JPG images.</strong></font><br>"; $file_upload_err=1; } } Is the coding correct? Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 27, 2009 Share Posted February 27, 2009 you are missing one } at the end of your script from what i can see you have 4 { and 3} Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 27, 2009 Share Posted February 27, 2009 is that script acvtually working? Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 27, 2009 Share Posted February 27, 2009 I am a bit confused doesn't actually say what you are doing with your images?? saving them to file? emailing them? saving them to a database? Quote Link to comment Share on other sites More sharing options...
sungpeng Posted February 27, 2009 Author Share Posted February 27, 2009 <?php include 'config2.php'; if($_POST[action]=="Update") { $check_result=0; $check_login=mysql_query("select * from users where email='$_POST[user]' and passwd='$_POST[pass]' and pid='$_GET[pid]'"); $check_result=mysql_num_rows($check_login); } if($check_result>0) { $folder_path="photo/$_GET[pid]/"; mkdir($folder_path,0777); if($imagefile) { $file_upload_err=0; $max_file_size="10000000"; // file size in bytes $max_file_size_kb="500"; $file_upload_err_msg=""; if($_FILES['imagefile']['size']>$max_file_size) { $file_upload_err_msg.="<font color='red'><strong>Image is bigger than the allowed file upload size ($max_file_size_kb kb).</strong></font><br>"; $file_upload_err=1; } if($imagefile && ($_FILES['imagefile']['type']!="image/jpeg" && $_FILES['imagefile']['type']!="image/pjpeg")) { $type=$_FILES['imagefile']['type']; $file_upload_err_msg.="<font color='red'><strong>Image is of a wrong file type.<br>Please upload only JPG images.</strong></font><br>"; $file_upload_err=1; } if($file_upload_err==0) { $get_pdt_info=mysql_query("select * from users where pid=$_GET[pid]"); $pdt_row=mysql_fetch_array($get_pdt_info); unlink("photo/$_GET[pid]/$pdt_row[imgdata]"); $pdt_id=$_GET[pid]; $folder_path="photo/$pdt_id/"; $image_path=$folder_path.$_FILES['imagefile']['name']; $img_name=$_FILES['imagefile']['name']; copy($imagefile, "$image_path"); $update_pdt=mysql_query("update users set imgdata='$img_name' where pid=$_GET[pid]"); } } } ?> <html> <title>Upload an image to a database</title> <body> <? $get_pdt_info=mysql_query("select * from instructor where pid=$_GET[pid]"); $pdt_row=mysql_fetch_array($get_pdt_info); ?> <h2>Update with new information</h2> <form name="form" enctype="multipart/form-data" method="post" action="<?php echo"$PHP_SELF?pid=$_GET[pid]"; ?>"> Please insert the images <input type='file' name='imagefile'><br> <br> USER : <input name="user"><br> PASSWORD : <input name="pass"><br> <input type="submit" name="action" value="Update"> </form> </body> </html> Take a look at my whole code. It can only insert one image. I want to insert 12 images, that the problem I am facing now. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted February 27, 2009 Author Share Posted February 27, 2009 I will have <input type='file' name='imagefile1'> <input type='file' name='imagefile2'> <input type='file' name='imagefile3'> and so on.. Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 27, 2009 Share Posted February 27, 2009 input form should be a php form must include the following <form name="nameofform" method="post" action="submit.php" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> also the input field should be <input type='file' name='imagefile[]'> <input type='file' name='imagefile[]'> <input type='file' name='imagefile[]'> Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 27, 2009 Share Posted February 27, 2009 when you mkdir what are you trying to make a folder with the usersid name? Quote Link to comment Share on other sites More sharing options...
sungpeng Posted February 27, 2009 Author Share Posted February 27, 2009 <?php include 'config2.php'; if($_POST[action]=="Update") { $check_result=0; $check_login=mysql_query("select * from users where email='$_POST[user]' and passwd='$_POST[pass]' and pid='$_GET[pid]'"); $check_result=mysql_num_rows($check_login); } if($check_result>0) { $folder_path="photo/$_GET[pid]/"; mkdir($folder_path,0777); for ($i = 0; $eimages = ${'imagefile'.$i}; $i++) { if($eimages) { $file_upload_err=0; $max_file_size="10000000"; // file size in bytes $max_file_size_kb="500"; $file_upload_err_msg=""; if($_FILES['imagefile'.$i]['size']>$max_file_size) { $file_upload_err_msg.="<font color='red'><strong>Image is bigger than the allowed file upload size ($max_file_size_kb kb).</strong></font><br>"; $file_upload_err=1; } if($eimages && ($_FILES['imagefile'.$i]['type']!="image/jpeg" && $_FILES['imagefile'.$i]['type']!="image/pjpeg")) { $type=$_FILES['imagefile'.$i]['type']; $file_upload_err_msg.="<font color='red'><strong>Image is of a wrong file type.<br>Please upload only JPG images.</strong></font><br>"; $file_upload_err=1; } } if($file_upload_err==0) { $get_pdt_info=mysql_query("select * from users where pid=$_GET[pid]"); $pdt_row=mysql_fetch_array($get_pdt_info); unlink("photo/$_GET[pid]/$pdt_row[imgdata]"); $pdt_id=$_GET[pid]; $folder_path="photo/$pdt_id/"; $image_path=$folder_path.$_FILES['imagefile'.$i]['name']; $img_name=$_FILES['imagefile'.$i]['name']; copy($eimages, "$image_path"); $update_pdt=mysql_query("update users set imgdata='$img_name' where pid=$_GET[pid]"); } } } ?> <html> <title>Upload an image to a database</title> <body> <? $get_pdt_info=mysql_query("select * from instructor where pid=$_GET[pid]"); $pdt_row=mysql_fetch_array($get_pdt_info); ?> <h2>Update with new information</h2> <form name="form" enctype="multipart/form-data" method="post" action="<?php echo"$PHP_SELF?pid=$_GET[pid]"; ?>"> Please insert the images <input type='file' name='imagefile[]'> <input type='file' name='imagefile[]'> <input type='file' name='imagefile[]'> <br> <br> USER : <input name="user"><br> PASSWORD : <input name="pass"><br> <input type="submit" name="action" value="Update"> </form> </body> </html> it doesn't work Quote Link to comment Share on other sites More sharing options...
sungpeng Posted February 27, 2009 Author Share Posted February 27, 2009 so that the photos is all in one folder belong to one person. Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 27, 2009 Share Posted February 27, 2009 am i right you are trying to create a form that will allow a registered user to upload up to 12 pictures from a form only if they are a registered user? and also make a folder with the users id as the folder name? if so please tell me this is easy but at the moment your script will not work it has so many errors let me know. i will try to help Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 27, 2009 Share Posted February 27, 2009 you want to be able to view these files later on? if so need to save the filename and type to the database? Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 27, 2009 Share Posted February 27, 2009 no it won't work give me ten fifteen minutes i will try to make something for you. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted February 27, 2009 Author Share Posted February 27, 2009 Yes the images name can save in imgdata1, imgdata2, imgdata3 etc in the database. The images itself saved in the unique folder of the user. The folders name is just numbers. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted February 27, 2009 Author Share Posted February 27, 2009 If the user did not upload any photo on image 5, the picture remain there. If the user upload a photo on image 6, then unlink the previous image 6 and upload the new one. Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 27, 2009 Share Posted February 27, 2009 you need to make an extra row for each file you need imgdata1 imgdata1type imgdata2 imgdata2type etc are these people already logged on before they get to this page or not? if not it is impossible to get the value of pid we would need to make them login first? Quote Link to comment Share on other sites More sharing options...
sungpeng Posted February 27, 2009 Author Share Posted February 27, 2009 yes it does, they already log on, but for extra security I ask them to key in their user name and password again. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted February 27, 2009 Author Share Posted February 27, 2009 <a href='insertphoto.php?pid=$doing[pid]'>insert photo</a> this is the previous pages code Quote Link to comment Share on other sites More sharing options...
sungpeng Posted February 27, 2009 Author Share Posted February 27, 2009 imgdata1type is it neccessary for exta "type" row as my previous one photo insertion does not have it? Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted February 27, 2009 Share Posted February 27, 2009 no not necessary sorry Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.