monkeyhead Posted July 5, 2010 Share Posted July 5, 2010 Hi there, I'm just working on a basic up-loader at the moment and I was wondering how to implement the script and form into the same file? I've tried the following below but it shows my error messages that I didn't specify the images , however the form still uploads. What I want to do is show the form and process it on the same page. I think its possible? <?php function imgprocess($f_name){ $max_size = 6291456; //Set the max image size in bytes default is approx 6MB $target_path = "uploads/"; //The path where the images are uploaded $target_path = $target_path . basename( $_FILES[$f_name]['name']); $type=$_FILES[$f_name]['type']; $size=$_FILES[$f_name]['size']; if ($size>$max_size) { echo "ERROR <br> the image size is too big<br><br>"; } if ($size<$max_size ) { if(move_uploaded_file($_FILES[$f_name]['tmp_name'], $target_path)) { echo("<table>"); echo "<tr><td><h1>However.....</h1>The file ". basename( $_FILES[$f_name]['name']). " has been uploaded</tr></td>"; echo '<tr><td><img src="' . $target_path . '" width="128" height="128"/></td></tr>'; echo "<td><ul>Img title:". basename( $_FILES[$f_name]['name']). "<br>"; echo "Img size:". basename( $_FILES[$f_name]['size']). " bytes<br>"; echo "File type:". basename( $_FILES[$f_name]['type']). "<br>"; echo("<a href=\"$target_path\"> Heres a link to your uploaded image!</a><br></td></tr>"); } else{ echo "Either you didnt specifiy an image for $f_name to upload or there was an error uploading the file, please try again!<br><br><br><br><br>"; } } } imgprocess(img1); imgprocess(img2); imgprocess(img3); imgprocess(img4); ?> <html><head><title> MJSEY image uploader</title> </head> <body><h3> MJSEY image uploader </h3> <img src="nt.jpg" alt="Angry face" width="128" height="128" align="middle" /> Please select the desired images you would like to upload:</br> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data"> <input type="file" name="img1" size="50"> </br> <input type="file" name="img2" size="50"> </br> <input type="file" name="img3" size="50"> </br> <input type="file" name="img4" size="50"> </br> <input type="submit" value="Submit!"> </form> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/ Share on other sites More sharing options...
phpSensei Posted July 5, 2010 Share Posted July 5, 2010 Missing your "$" signs there. imgprocess(img1);imgprocess(img2);imgprocess(img3);imgprocess(img4); also where is that you are declaring a value for these variables? $img1 $img2 ? Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/#findComment-1081522 Share on other sites More sharing options...
Pikachu2000 Posted July 5, 2010 Share Posted July 5, 2010 In addition to what phpSensei mentioned, I assume you mean you're getting the errors on the initial page load, correct? Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/#findComment-1081528 Share on other sites More sharing options...
jcbones Posted July 5, 2010 Share Posted July 5, 2010 Try: if(isset($_POST)) { imgprocess(img1); imgprocess(img2); imgprocess(img3); imgprocess(img4); } Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/#findComment-1081539 Share on other sites More sharing options...
phpSensei Posted July 5, 2010 Share Posted July 5, 2010 Try: if(isset($_POST)) { imgprocess(img1); imgprocess(img2); imgprocess(img3); imgprocess(img4); } But your still missing "$" i dont se where you are even getting img1 from anyways.. They have to be declared somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/#findComment-1081549 Share on other sites More sharing options...
monkeyhead Posted July 5, 2010 Author Share Posted July 5, 2010 In addition to what phpSensei mentioned, I assume you mean you're getting the errors on the initial page load, correct? Correct. Also this is where I'm getting the variables from <input type="file" name="img1" size="50"> </br> <input type="file" name="img2" size="50"> </br> <input type="file" name="img3" size="50"> </br> <input type="file" name="img4" size="50"> Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/#findComment-1081564 Share on other sites More sharing options...
phpSensei Posted July 5, 2010 Share Posted July 5, 2010 In addition to what phpSensei mentioned, I assume you mean you're getting the errors on the initial page load, correct? Correct. Also this is where I'm getting the variables from <input type="file" name="img1" size="50"> </br> <input type="file" name="img2" size="50"> </br> <input type="file" name="img3" size="50"> </br> <input type="file" name="img4" size="50"> Those aren't variables. Those are the name of your input fields... $img1 = $_POST['img1']['name']; imgprocess($img1); Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/#findComment-1081571 Share on other sites More sharing options...
monkeyhead Posted July 5, 2010 Author Share Posted July 5, 2010 In addition to what phpSensei mentioned, I assume you mean you're getting the errors on the initial page load, correct? Correct. Also this is where I'm getting the variables from <input type="file" name="img1" size="50"> </br> <input type="file" name="img2" size="50"> </br> <input type="file" name="img3" size="50"> </br> <input type="file" name="img4" size="50"> Those aren't variables. Those are the name of your input fields... $img1 = $_POST['img1']['name']; imgprocess($img1); I tried that but still have the same issue Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/#findComment-1081576 Share on other sites More sharing options...
phpSensei Posted July 5, 2010 Share Posted July 5, 2010 try this <?php function imgprocess($f_name){ $max_size = 6291456; //Set the max image size in bytes default is approx 6MB $target_path = "uploads/"; //The path where the images are uploaded $target_path = $target_path . basename( $_FILES[$f_name]['name']); $type=$_FILES[$f_name]['type']; $size=$_FILES[$f_name]['size']; if ($size>$max_size) { echo "ERROR <br> the image size is too big<br><br>"; } if ($size<$max_size ) { if(move_uploaded_file($_FILES[$f_name]['tmp_name'], $target_path)) { echo "<table>"; echo "<tr><td><h1>However.....</h1>The file ".basename( $_FILES[$f_name]['name'])." has been uploaded</tr></td>"; echo '<tr><td><img src="' . $target_path . '" width="128" height="128"/></td></tr>'; echo "<td><ul>Img title:". basename( $_FILES[$f_name]['name']). "<br>"; echo "Img size:". basename( $_FILES[$f_name]['size']). " bytes<br>"; echo "File type:". basename( $_FILES[$f_name]['type']). "<br>"; echo("<a href=\"$target_path\"> Heres a link to your uploaded image!</a><br></td></tr>"); }else{ echo "Either you didnt specifiy an image for $f_name to upload or there was an error uploading the file, please try again!<br><br> <br><br><br>"; } } } if(isset($_POST['submit'])){ $image1 = $_FILES['img1']; $image2 = $_FILES['img2']; $image3 = $_FILES['img3']; $image3 = $_FILES['img4']; imgprocess($image1); imgprocess($image2); imgprocess($image3); imgprocess($image4); } ?> <html> <head> <title> MJSEY image uploader</title> </head> <body> <h3> MJSEY image uploader </h3> <img src="nt.jpg" alt="Angry face" width="128" height="128" align="middle" /> Please select the desired images you would like to upload:</br> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data"> <input type="file" name="img1" size="50"> </br> <input type="file" name="img2" size="50"> </br> <input type="file" name="img3" size="50"> </br> <input type="file" name="img4" size="50"> </br> <input type="submit" name ="submit" value="Submit!"> </form> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/#findComment-1081586 Share on other sites More sharing options...
monkeyhead Posted July 5, 2010 Author Share Posted July 5, 2010 try this <?php function imgprocess($f_name){ $max_size = 6291456; //Set the max image size in bytes default is approx 6MB $target_path = "uploads/"; //The path where the images are uploaded $target_path = $target_path . basename( $_FILES[$f_name]['name']); $type=$_FILES[$f_name]['type']; $size=$_FILES[$f_name]['size']; if ($size>$max_size) { echo "ERROR <br> the image size is too big<br><br>"; } if ($size<$max_size ) { if(move_uploaded_file($_FILES[$f_name]['tmp_name'], $target_path)) { echo "<table>"; echo "<tr><td><h1>However.....</h1>The file ".basename( $_FILES[$f_name]['name'])." has been uploaded</tr></td>"; echo '<tr><td><img src="' . $target_path . '" width="128" height="128"/></td></tr>'; echo "<td><ul>Img title:". basename( $_FILES[$f_name]['name']). "<br>"; echo "Img size:". basename( $_FILES[$f_name]['size']). " bytes<br>"; echo "File type:". basename( $_FILES[$f_name]['type']). "<br>"; echo("<a href=\"$target_path\"> Heres a link to your uploaded image!</a><br></td></tr>"); }else{ echo "Either you didnt specifiy an image for $f_name to upload or there was an error uploading the file, please try again!<br><br> <br><br><br>"; } } } if(isset($_POST['submit'])){ $image1 = $_FILES['img1']; $image2 = $_FILES['img2']; $image3 = $_FILES['img3']; $image3 = $_FILES['img4']; imgprocess($image1); imgprocess($image2); imgprocess($image3); imgprocess($image4); } ?> <html> <head> <title> MJSEY image uploader</title> </head> <body> <h3> MJSEY image uploader </h3> <img src="nt.jpg" alt="Angry face" width="128" height="128" align="middle" /> Please select the desired images you would like to upload:</br> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data"> <input type="file" name="img1" size="50"> </br> <input type="file" name="img2" size="50"> </br> <input type="file" name="img3" size="50"> </br> <input type="file" name="img4" size="50"> </br> <input type="submit" name ="submit" value="Submit!"> </form> </body></html> That worked, however i now get a new error : Warning: Illegal offset type in /img/index.php on line 8 Warning: Illegal offset type in /img/index.php on line 9 Warning: Illegal offset type in /img/index.php on line 10 Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/#findComment-1081589 Share on other sites More sharing options...
phpSensei Posted July 5, 2010 Share Posted July 5, 2010 did it upload the images? In this case I would ignore this.. It has to do with the array keys actually.. Array s and object s can not be used as keys. Doing so will result in a warning: Illegal offset type. Anyone correct me if i'm wrong, still a little rusty with php here since I havnt been coding for a few months. I believe its just the way your going about uploading this thing. A much easier way of doing this would have been to just name each input file field as "img[]" instead of "img1,img2,img..etc".. But this error isn't a problem really, you can do a if(isset()) for the array but thats a pain in the ass. You can also do error_reporting(0) so users can see it... Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/#findComment-1081591 Share on other sites More sharing options...
monkeyhead Posted July 5, 2010 Author Share Posted July 5, 2010 It didnt upload either Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/#findComment-1081603 Share on other sites More sharing options...
phpSensei Posted July 5, 2010 Share Posted July 5, 2010 It didnt upload either AHH $target_path = $target_path . basename( $_FILES[$f_name]['name']); You see theres the problem How was i so blind? $f_name doesnt go into $_FILES because thats like saying $_FILES[$_FILES['img1']]]['name'] You understand what i am saying? Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/#findComment-1081610 Share on other sites More sharing options...
phpSensei Posted July 5, 2010 Share Posted July 5, 2010 Here yuo go champ .... *erm* trust me I am still rusty with php, some obvious mistakes I dont always catch, I am getting good again though. And uhhh I was jus ttesting you, trying to see if you can find the mistake... lol just keeding Disregard what I said about the Array Keys and Offset, that was the problem... <?php function imgprocess($f_name){ $max_size = 6291456; //Set the max image size in bytes default is approx 6MB $target_path = "uploads/"; //The path where the images are uploaded $target_path = $target_path . basename( $f_name['name']); $type=$f_name['type']; $size=$f_name['size']; if ($size>$max_size) { echo "ERROR <br> the image size is too big<br><br>"; } if ($size<$max_size ) { if(move_uploaded_file($f_name['tmp_name'], $target_path)) { echo "<table>"; echo "<tr><td><h1>However.....</h1>The file ".basename( $f_name['name'])." has been uploaded</tr></td>"; echo '<tr><td><img src="' . $target_path . '" width="128" height="128"/></td></tr>'; echo "<td><ul>Img title:". basename( $f_name['name']). "<br>"; echo "Img size:". basename($f_name['size']). " bytes<br>"; echo "File type:". basename($f_name['type']). "<br>"; echo("<a href=\"$target_path\"> Heres a link to your uploaded image!</a><br></td></tr>"); }else{ echo "Either you didnt specifiy an image for ".$f_name['name']." to upload or there was an error uploading the file, please try again!<br><br> <br><br><br>"; } } } if(isset($_POST['submit'])){ $image1 = $_FILES['img1']; $image2 = $_FILES['img2']; $image3 = $_FILES['img3']; $image3 = $_FILES['img4']; imgprocess($image1); imgprocess($image2); imgprocess($image3); imgprocess($image4); } ?> <html> <head> <title> MJSEY image uploader</title> </head> <body> <h3> MJSEY image uploader </h3> <img src="nt.jpg" alt="Angry face" width="128" height="128" align="middle" /> Please select the desired images you would like to upload:</br> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data"> <input type="file" name="img1" size="50"> </br> <input type="file" name="img2" size="50"> </br> <input type="file" name="img3" size="50"> </br> <input type="file" name="img4" size="50"> </br> <input type="submit" name ="submit" value="Submit!"> </form> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/#findComment-1081611 Share on other sites More sharing options...
monkeyhead Posted July 5, 2010 Author Share Posted July 5, 2010 Cheers for that it worked Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/#findComment-1081621 Share on other sites More sharing options...
phpSensei Posted July 5, 2010 Share Posted July 5, 2010 Cheers for that it worked Cheers man Mark thread as solved plz Quote Link to comment https://forums.phpfreaks.com/topic/206800-php-script-and-form-in-the-same-page/#findComment-1081625 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.