ggw Posted March 30, 2012 Share Posted March 30, 2012 Hello I am having problems uploading an image through a HTML form. I want the image to be uploaded to the server and the image name to be written to the mysql database. Below is the code I am using: <?php if (isset($_POST['add'])){ echo "<br /> add value is true"; $name = $_POST['name']; $description = $_POST['description']; $price = $_POST['price']; $category_id = $_POST['category_name']; $image = $_FILES['image']['name']; //file path of the image upload $filepath = "../images/"; //mew name for the image upload $newimagename = $name; //new width for the image $newwidth = 100; //new height for the image $newheight = 100; include('../includes/image-upload.php'); mysql_query("INSERT INTO item (item_name, item_description, item_price, item_image) VALUES ('$name','$description','$price','$image')"); ?> Here is the image-upload.php file code: <?php //assigns the file to the image $image =$_FILES["image"]["name"]; $uploadedfile =$_FILES["image"]["tmp_name"]; if ($image) { //retrieves the extension type from image upload $extension = getextension($image); //converts extension to lowercase $extension = strtolower($extension); //create image from uploaded file type if($extension=="jpg" || $extension=="jpeg") { $uploadedfile = $_FILES['image']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); }else if($extension=="png") { $uploadedfile = $_FILES['image']['tmp_name']; $src = imagecreatefrompng($uploadedfile); }else{ $src = imagecreatefromgif($uploadedfile); } //creates a list of the width and height of the image list($width,$height)=getimagesize($uploadedfile); //adds color to the image $tmp = imagecreatetruecolor($newwidth,$newheight); //create image imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); //set file name $filename = $filepath.$newimagename.".".$extension; $imagename = $newimagename.".".$extension; //uploads new file with name to the chosen directory imagejpeg($tmp,$filename,100); //empty variables imagedestroy($src); imagedestroy($tmp); } ?> Any help would be appreciated, fairly new to all this! Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/260003-image-upload-to-server-writing-image-name-to-mysql/ Share on other sites More sharing options...
cpd Posted March 30, 2012 Share Posted March 30, 2012 I can't see you uploading the file anywhere in your script. Take a look at the move_uploaded_file() function here: http://www.php.net/move_uploaded_file Quote Link to comment https://forums.phpfreaks.com/topic/260003-image-upload-to-server-writing-image-name-to-mysql/#findComment-1332656 Share on other sites More sharing options...
ggw Posted March 30, 2012 Author Share Posted March 30, 2012 Please could you give me an example of this, as I thought $uploadedfile was doing this. Sorry to be a pain! Thank you!!! Quote Link to comment https://forums.phpfreaks.com/topic/260003-image-upload-to-server-writing-image-name-to-mysql/#findComment-1332658 Share on other sites More sharing options...
AyKay47 Posted March 30, 2012 Share Posted March 30, 2012 Please could you give me an example of this, as I thought $uploadedfile was doing this. Sorry to be a pain! Thank you!!! there are examples on the page that you were linked to. In the given script, you are simply setting $uploadedfile to name of the file in the tmp directory. This has nothing to do with actually uploading the file to your server. Quote Link to comment https://forums.phpfreaks.com/topic/260003-image-upload-to-server-writing-image-name-to-mysql/#findComment-1332667 Share on other sites More sharing options...
ggw Posted March 30, 2012 Author Share Posted March 30, 2012 I have added move_uploaded_file($tmp_name, "$filepath/$image"); to the image-upload.php but this doesn't work! My head is completely baffled! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/260003-image-upload-to-server-writing-image-name-to-mysql/#findComment-1332685 Share on other sites More sharing options...
AyKay47 Posted March 30, 2012 Share Posted March 30, 2012 please post the updated code, along with any errors you are receiving. Quote Link to comment https://forums.phpfreaks.com/topic/260003-image-upload-to-server-writing-image-name-to-mysql/#findComment-1332711 Share on other sites More sharing options...
ggw Posted March 30, 2012 Author Share Posted March 30, 2012 I have put it back to what it was originally now, I don't know what to do, been trying for two days. I looked at other forums, websites and books. Everything I do just doesn't seem to work! My current code is : <?php //This is the directory where images will be saved $target = "images/"; $target = $target . basename( $_FILES['image']['name']); if (isset($_POST['add'])){ echo "<br /> add value is true"; $name = $_POST['name']; $description = $_POST['description']; $price = $_POST['price']; $category_id = $_POST['category_name']; $image = ($_FILES['image']['name']); move_uploaded_file($_FILES['image']['tmp_name'], $target); mysql_query ("INSERT INTO item (item_name, item_description, item_price, item_image) VALUES ('$name','$description','$price','$image')"); $item_id = mysql_insert_id(); echo "item id is: " . $item_id; mysql_query("INSERT INTO links (item_id, category_id) VALUES ('$item_id', '$category_id' )"); } ?> The image-upload.php is the same as the post above. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/260003-image-upload-to-server-writing-image-name-to-mysql/#findComment-1332735 Share on other sites More sharing options...
AyKay47 Posted March 30, 2012 Share Posted March 30, 2012 if you have error_reporting set to -1 and display_errors on, you should be receiving an error upon upload failure, and any other errors for that matter. Quote Link to comment https://forums.phpfreaks.com/topic/260003-image-upload-to-server-writing-image-name-to-mysql/#findComment-1332753 Share on other sites More sharing options...
ggw Posted April 1, 2012 Author Share Posted April 1, 2012 The error is : Fatal error: Call to undefined function getextension() in image-upload.php on line 9 Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/260003-image-upload-to-server-writing-image-name-to-mysql/#findComment-1333343 Share on other sites More sharing options...
AyKay47 Posted April 2, 2012 Share Posted April 2, 2012 The error is : Fatal error: Call to undefined function getextension() in image-upload.php on line 9 Thanks! than getextension() does not exist at the time that it is called. Quote Link to comment https://forums.phpfreaks.com/topic/260003-image-upload-to-server-writing-image-name-to-mysql/#findComment-1333419 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.