Placing a variable of existing file in db into muliple upload form
I've implemented the multiple upload script into a shopping cart editor to use for uploading a main image and thumbnail image. Thanks to the OP for this, its been a big help on my project. It works fine for inserting a new product, but I'm having trouble using the script for updating the database. Currently when the user goes to the page update an existing product they have to reselect an image from their local drive, rather than the files being automatically selected from the database. So my question is where do I place the variable for the existing files in database so they display automatically on the update page? (rather than it saying "no file selcted" it has the relevant image already stored in the form from the db).
I've implemented the multiple upload script into a shopping cart editor to use for uploading a main image and thumbnail image. Thanks to the OP for this, its been a big help on my project. It works fine for inserting a new product, but I'm having trouble using the script for updating the database. Currently when the user goes to the page update an existing product they have to reselect an image from their local drive, rather than the files being automatically selected from the database. So my question is where do I place the variable for the existing files in database so they display automatically on the update page? (rather than it saying "no file selcted" it has the relevant image already stored in the form from the db).
Actually second thoughts... What I'd like to do is display the current image in the database on the update page, then when submitting the updated details, if no files are selected, instead of giving the error it just bypasses the upload and just updates the rest of the database. How do I get the script to bypass the upload if no file is selected instead of giving the error?
I figured it out if anyone wants it for reference...
update.php
Code:
$prodId=$_POST['prodId'];
$prodName=$_POST['ud_name'];
$price=$_POST['ud_price'];
$description=$_POST['ud_description'];
$subcat=$_POST['ud_subcategory_new'];
$subcat3=$_POST['ud_subcategory_new3'];
$subcat4=$_POST['ud_subcategory_new4'];
$updated2 = '/updated2.php?prodId='. $prodId .'&ud_name='. $prodName.'&ud_price='.$price.'&ud_description='.$description.'&ud_subcategory='.$subcat.'&ud_subcategory2='.$subcat2.'&ud_subcategory3='.$subcat3.'&ud_subcategory4='.$subcat4 ;
....
// if no file selected go to alternative update file
($_FILES[$fieldname]['error'] == 0)
or header('Location: ' . $updated2);
..... Rest of file adding images and other content to db
Then alternate update file that doesn't update images....
Code:
$prodId=$_GET['prodId'];
$prodName=$_GET['ud_name'];
$price=$_GET['ud_price'];
$description=$_GET['ud_description'];
$subcat=$_GET['ud_subcategory_new'];
$subcat3=$_GET['ud_subcategory_new3'];
$subcat4=$_GET['ud_subcategory_new4'];
$query = "UPDATE products SET prodName='$prodName', price='$price', description='$description', subcat='$subcat', subcat2='$subcat2', subcat3='$subcat3', subcat4='$subcat4' WHERE prodId='$prodId'";
mysql_query($query) or die (mysql_error());
mysql_close($dbl);
$uploadSuccess = '/upload.success.php';
header('Location: ' . $uploadSuccess);
having searched around for a solution, I couldn't find one... so here's the problem:
I have my own website, which is essentially my portfolio: www.blueeyelabs.com
I need an easy way of adding images, properly.
I have a sort of way already that involves a php page to add the MySQL records and an automator script to upload etc the images.
What I want is a php page that:
-Uploads the images
-Resizes and saves them in different locations according to requirement
-Adds the records to the database
-Adds a news item about the image
Well, this is quite hard evidently, since I can't manage to do it properly.
The script for adding records definitely works, I've tried.
But the problem (I think) is to do with permissions, since I can't think of any reason why my script doesn't work (I've never even got a simpler version to work); it seems to break down on trying to save an image to a directory.
I have tried chmod() commands, but they don't work, for some obscure reason, so here is the script (well, the image part, the rest of it I haven't bothered posting since it works).
The errors I get are either:
DL means "download": ie downloadable image
viewer means the image that will be viewed as a big but not full size image
thumbnail means just that; a thumbnail.
Here is the script:
PHP Code:
//image functions and upload scripts
//connect to ftp:
$ftpCon = ftp_connect('server') or die('connection failed (ftp)');
echo 'FTP connection successful <br/>';
$ftpResult = ftp_login($ftpCon, "usr", "pwd") or die('login failed (ftp)');
echo 'FTP login successful <br/>';
//we want a thumbnail and a full image, and maybe a downloadable image.
//so let's resize.
echo 'Image Progress: <br/>';
//first copy the *download* file, if required
if($dl == 1) {
$upload = ftp_put($ftpCon, $dlFile, $tmpFile, 0755) or die('ftp of dl file failed');
echo 'DL image successfully created <br/>';
} else {
echo 'DL image not required <br/>';
}
//then let's resize and copy the *Viewer* image:
$src = imagecreatefromjpeg($tmpFile);
list($width,$height) = getimagesize($tmpFile);
//define target width/height
$tWidth = 800;
$tHeight = 600;
//check if width or height are bigger, and resize accordingly.
if($width > $height) {
$newWidth = $tWidth;
$newHeight = ($height / $width) * $height;
} elseif($height > $width) {
$newHeight = $tHeight;
$newWidth = ($width / $height) * $tWidth;
} else {
die('unable to resize viewer image');
}
//create new image
$tmp2 = imagecreatetruecolor($newWidth, $newHeight);
//ready, set, resize!
imagecopyresampled($tmp2, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
//copy to directory
imagejpeg($tmp2, $imageFile, 100) or die('unable to copy viewer image');
//destroy all temporary images
imagedestroy($src);
imagedestroy($tmp2);
echo 'Viewer image successfully created <br/>';
//onwards! To the thumbnail!
$src = imagecreatefromjpeg($tmpFile);
list($width,$height) = getimagesize($tmpFile);
//define pre-crop target width/height
$tWidth = 800;
$tHeight = 600;
//check if width or height are bigger, and resize accordingly.
if($width > $height) {
$newWidth = $tWidth;
$newHeight = ($height / $width) * $height;
} elseif($height > $width) {
$newHeight = $tHeight;
$newWidth = ($width / $height) * $tWidth;
} else {
die('unable to resize pre-crop image');
}
//create new image
$tmp2 = imagecreatetruecolor($tWidth, $tHeight);
//copy new pre-crop image:
imagecopyresampled($tmp2, $src, 0, 0, 0, 0, $tWidth, $tHeight, $width, $height);
$width = $tWidth;
$height = $tHeight;
//now we need crop target width and height:
$tWidth = 100;
$tHeight = 100;
//in order to crop we need to find out where to start:
//so, find halfway through the width/height and then subtract the target width/height:
$cropStartX = ($width / 2) - $tWidth;
$cropStartY = ($height / 2) - $tHeight;
//ready, set, crop!
imagecopyresampled($tmp2, $src, 0, 0, $cropStartX, $cropStartY, $tWidth, $tHeight, $width, $height);
//copy to directory
imagejpeg($tmp2, $thumbFile, 100) or die('unable to copy thumbnail');
//destroy temp images
imagedestroy($src);
imagedestroy($tmp2);
Thats a URL, not a remote file path. You can't "put" a file by URL; the receiving machine needs to know where to put the file within its filing system, not where to put it in cyberspace.
oh, I see, do I use $_SERVER[] for that then?
The problem is that I can't use DOCUMENT_ROOT because my upload script is in a completely different directory (~/admin) to my image directory (~/portfolio).
This file upload script works very well. Thanks for providing the basics of it. It works flawlessly. I did make a few changes. I pass the file name on to the success.upload.php file, and then echo the URL into a text box (i.e. <input type='text' value='$filename' > ) . The user can then select the URL easily, and use it to link too, or whatever.
My website has 'hot link protection' so you can't link images remotely. Can only link within the website. So if someone uploads a file it can only be viewed in the message forum. This is useful for those that don't have a good place to upload pictures too, but want an image in a message.
I simply unzipped the file and uploaded them as it is. As you can see at the URL, the folder is located at the root. Try the link, the script isn't working.
1. Do I have to add and edit some scripts in there?
2. If not, What the problem I'm facing?
Michael
P.S. except, I changed this
PHP Code:
// set a max file size for the html upload form
$max_file_size = 3000000000000; // size in bytes
// since the images might be bigger than the normal ones
Last edited by Michaelttkk; 11-13-2007 at 10:22 PM.
it seems to be getting a "not an image" problem which would suggest that you're not actually playing with the image itself, since I got a rather odd name for the temporary file... If this isn't a public form it might be worth trying to reduce some of that validation just to see if it works...
getimagesize() cant read the image. Remove the '@' symbol in front of getimagesize and see if the script raises an error. If not, raise the error_reporting level until one is shown. It may be a permission problem.
Originally Posted by blue-eye-labs
I got a rather odd name for the temporary file
Odd names are par for the course with temporary files.
@bokeh:
I have wondered why you put the @ sign in front of a great many of your commands...
What I meant by odd is that there wasn't a file extension.... but maybe that's just the way it is with temp files
Bookmarks