kev wood Posted November 13, 2008 Share Posted November 13, 2008 i have created an image upload page to resize and store image into a folder on the server. i want these images to be linked to an article that is uploaded at the same time. i have set the article page up and this is storing the data how i would like but i now need to store the image so that when the articles are viewed on the site the image accompanies the article. my mind has gone blank as how i can link the two together. Quote Link to comment https://forums.phpfreaks.com/topic/132540-solved-image-upload-with-forms-help/ Share on other sites More sharing options...
JonnoTheDev Posted November 13, 2008 Share Posted November 13, 2008 Add a database field to store the image filename in your articles table. Add a file upload field into the form that creates the article: <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="image" id="image" size="70" maxlength="255"> </form> You can then upload the file from: $_FILES['image']['name'] Checkout some image uploading tutorials. To resize your image you can use php's GD library functions (must have these available in your php setup) or download and install imagemagick Quote Link to comment https://forums.phpfreaks.com/topic/132540-solved-image-upload-with-forms-help/#findComment-689200 Share on other sites More sharing options...
kev wood Posted November 13, 2008 Author Share Posted November 13, 2008 i have ready created the upload section and it works fine i now just need to add some code which works out what the last record added was to the db and then updates this record with the image. my brain has just kicked back into gear i think it has woken up again. i am now looking into how to get it to find the last record and then run a simple update using the last rows id number. here is my upload code if anyone would like to use it. //define a maxim size for the uploaded images define ("MAX_SIZE","500"); // define the width and height for the thumbnail // note that theese dimmensions are considered the maximum dimmension and are not fixed, // because we have to keep the image ratio intact or it will be deformed define ("WIDTH","150"); define ("HEIGHT","120"); // this is the function that will create the thumbnail image from the uploaded image // the resize will be done considering the width and height defined, but without deforming the image function make_thumb($img_name,$filename,$new_w,$new_h) { //get image extension. $ext=getExtension($img_name); //creates the new image using the appropriate function from gd library if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext)) $src_img=imagecreatefromjpeg($img_name); if(!strcmp("png",$ext)) $src_img=imagecreatefrompng($img_name); if(!strcmp("gif",$ext)) $src_img=imagecreatefromgif($img_name); //gets the dimmensions of the image $old_x=imageSX($src_img); $old_y=imageSY($src_img); // next we will calculate the new dimmensions for the thumbnail image // the next steps will be taken: // 1. calculate the ratio by dividing the old dimmensions with the new ones // 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable // and the height will be calculated so the image ratio will not change // 3. otherwise we will use the height ratio for the image // as a result, only one of the dimmensions will be from the fixed ones $ratio1=$old_x/$new_w; $ratio2=$old_y/$new_h; if($ratio1>$ratio2) { $thumb_w=$new_w; $thumb_h=$old_y/$ratio1; } else { $thumb_h=$new_h; $thumb_w=$old_x/$ratio2; } // we create a new image with the new dimmensions $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); // resize the big image to the new created one imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); // output the created image to the file. Now we will have the thumbnail into the file named by $filename if(!strcmp("png",$ext)) imagepng($dst_img,$filename); else imagejpeg($dst_img,$filename); if (!strcmp("gif",$ext)) imagegif($dst_img,$filename); //destroys source and destination images. imagedestroy($dst_img); imagedestroy($src_img); } // This function reads the extension of the file. // It is used to determine if the file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } // This variable is used as a flag. The value is initialized with 0 (meaning no error found) //and it will be changed to 1 if an error occures. If the error occures the file will not be uploaded. $errors=0; // checks if the form has been submitted if(isset($_POST['Submit'])) { //reads the name of the file the user submitted for uploading $image=$_FILES['cons_image']['name']; // if it is not empty if ($image) { // get the original name of the file from the clients machine $filename = stripslashes($_FILES['cons_image']['name']); // get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); // if it is not a known extension, we will suppose it is an error, print an error message //and will not upload the file, otherwise we continue if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo '<h1>Unknown extension! Please use .gif, .jpg or .png files only.</h1>'; $errors=1; } else { // get the size of the image in bytes // $_FILES[\'image\'][\'tmp_name\'] is the temporary filename of the file in which //the uploaded file was stored on the server $size=getimagesize($_FILES['cons_image']['tmp_name']); $sizekb=filesize($_FILES['cons_image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($sizekb > MAX_SIZE*1024) { echo '<h1>You have exceeded the 1MB size limit!</h1>'; $errors=1; } Quote Link to comment https://forums.phpfreaks.com/topic/132540-solved-image-upload-with-forms-help/#findComment-689211 Share on other sites More sharing options...
JonnoTheDev Posted November 13, 2008 Share Posted November 13, 2008 Strange way of doing it. When creating an article you should add the upload to the article creation form, that way you insert the article along with the image filename at the same time. To do it your way you either need to obtain the article id from a URL param, i.e. add image to article Or, if your upload comes directly after the article is inserted the id can be obtained using mysql_insert_id() Quote Link to comment https://forums.phpfreaks.com/topic/132540-solved-image-upload-with-forms-help/#findComment-689218 Share on other sites More sharing options...
kev wood Posted November 13, 2008 Author Share Posted November 13, 2008 yes the upload comes directly after the article. the reason for doing it this was because of those lovable clients. i was originally asked to create a content management system for a flash site i had built now they have decided they would like images to accompany the news articles that they are uploading. i had already built the CMS so i am now jusy ading this section to it instead of having it done on one form. Quote Link to comment https://forums.phpfreaks.com/topic/132540-solved-image-upload-with-forms-help/#findComment-689220 Share on other sites More sharing options...
JonnoTheDev Posted November 13, 2008 Share Posted November 13, 2008 Get the id with mysql_insert_id() after the article is inserted. You can then use this in a URL param on your upload page. Store in a hidden form field and then use to update your article database record. Quote Link to comment https://forums.phpfreaks.com/topic/132540-solved-image-upload-with-forms-help/#findComment-689222 Share on other sites More sharing options...
kev wood Posted November 13, 2008 Author Share Posted November 13, 2008 i dont think that is going to work i ahve just had a go and i an getting nothing posted back to the page which needs the last id number. here is the code i have used to try and get the last id number $sql= "INSERT INTO $table (date, title, article) VALUES ('$a', '$b', '$c')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } $last_id=mysql_insert_id(); echo "<h2>1 record added</h2>"; and to send it back i have used a hidden form <form action="index.php"> <input type="hidden" name="last_id" value="<? echo $last_id ?>"> <input type="hidden"> </form> and to see if the variable is being passed back to the page i am using $last_id = $_POST['last_id']; echo $last_id; nothing is being echoed out on the page. if i have gone wrong please point it out to me. aint used mysql_insert_id before Quote Link to comment https://forums.phpfreaks.com/topic/132540-solved-image-upload-with-forms-help/#findComment-689264 Share on other sites More sharing options...
JonnoTheDev Posted November 13, 2008 Share Posted November 13, 2008 Are you not sending the user to another page to do the image upload? If not you should be. article-insert.php ============= $sql= "INSERT INTO $table (date, title, article) VALUES ('$a', '$b', '$c')"; mysql_query($sql,$con); // redirect user header("Location:upload.php?id=".mysql_insert_id()); exit(); upload.php ========== $databaseId = $_GET['id']; Quote Link to comment https://forums.phpfreaks.com/topic/132540-solved-image-upload-with-forms-help/#findComment-689285 Share on other sites More sharing options...
kev wood Posted November 13, 2008 Author Share Posted November 13, 2008 the user enters the information they want to be entered then they press submit then the user is directed to a new page which tells them weather the article was added or not. this page then redirects the user to the original page they were on which also has the option of adding the image to the db. once they are back on this page i need the number from the mysql_insert_id() to preform an update on the db where the id is equal to the id number passed back to the page. not all articles will have images with them so that is another reason for having the upload happen after the article upload. i redirect the user using header as it throughs up errors. i have done the redirect using meta tags so i have changed this line to this <meta http-equiv="Refresh" content="5;URL=index.php?$last_id"> and used the GET method to retrieve the data on the new page. still i cannot echo this number out on the page. Quote Link to comment https://forums.phpfreaks.com/topic/132540-solved-image-upload-with-forms-help/#findComment-689302 Share on other sites More sharing options...
kev wood Posted November 13, 2008 Author Share Posted November 13, 2008 i have been researching and trying to get it to work using the max function in mysql but still with no joy here is the code i have so far. $run=mysql_query('select max(id) from internal'); $lastid = mysql_fetch_row($run); $lastid=$lastid[0]; $sql="INSERT INTO internal (image) VALUE $consname2 WHERE id= $lastid"; $query = mysql_query($sql); i no this is an insert into function but i have also tried $run=mysql_query('select max(id) from internal'); $lastid = mysql_fetch_row($run); $lastid=$lastid[0]; $sql="UPDATE internal SET image= $consname2 WHERE id= $lastid"; $query = mysql_query($sql); with the same results i can get it to echo out the highest number in the id column but it will not update the column. when i try to update it the image is uploaded to the to the server put the path to the file is not stored in the db? if you want a link to the example i am working on i will pm it to you. Quote Link to comment https://forums.phpfreaks.com/topic/132540-solved-image-upload-with-forms-help/#findComment-689407 Share on other sites More sharing options...
kev wood Posted November 13, 2008 Author Share Posted November 13, 2008 it now works here is the old code $run=mysql_query('select max(id) from internal'); $lastid = mysql_fetch_row($run); $lastid=$lastid[0]; $sql="UPDATE internal SET image= $consname2 WHERE id= $lastid"; $query = mysql_query($sql); spot the difference with the new code $run=mysql_query('select max(id) from internal'); $lastid = mysql_fetch_row($run); $lastid=$lastid[0]; $sql="UPDATE internal SET image= '$consname2' WHERE id= '$lastid'"; $query = mysql_query($sql); Quote Link to comment https://forums.phpfreaks.com/topic/132540-solved-image-upload-with-forms-help/#findComment-689421 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.