hi i am trying to upload an image but it doesn’t:( work I have been trying for ages but without any luck. I was wondering if anyone would be kindly enough to take a look at it.
Basicly i’m getting my echo error message “problem uploading image” displayed constantly, even before i try upload it keeps saying it. It is also not uploading.
my database
id (int5) auto_increment
image_name (varchar100)
image (blob)
Here is my code i have two php files. I will attcah them also to make it easier to look at
the error is at the database, however you will need to move the file to a directory you want (move_uploaded_file) as I can’t see this in the code.
For the error, after this line: echo “Problem uploading image.”;
add this: echo mysql_error();
so you can see what is the error.
yes, ive been using it for the users to register and it works strange :s lol. i have managed to edit the code so the form goes to an action page instead of the php being on the same page. I have managed to get it to successfully add to the database however it will not display here is my 3 php files
I usually keep the images in the filesystem and store a reference to it in the database thus keeping the db small in size…
Are you sure you are getting a unique id with the query, using an empty string? Try changing it to this:
INSERT INTO products VALUES (NULL,‘$image_name’,‘$image’)
It happen that I ofter use the following script, so this solution is tested & debbuged. Check if you can use it. The difference is that I store my files in folder, and only the file name is stored in the database:
Here is the code:
$image_id=mysql_real_escape_string($_POST['image_id']);
$artid=mysql_real_escape_string($_POST['artid']);
$sorder=mysql_real_escape_string($_POST['sorder']);
$visible=mysql_real_escape_string($_POST['visible']);
$filename =mysql_real_escape_string($_FILES['filename']['name']);
// find size
if ($_FILES['filename']['size']>204800) {
//go to error page
Header ("Location: errorupload.php?size=over");
} else {
//else go to ok page
Header ("Location: articleimages.php");
}
// find file extension
$valid_extensions = array('gif', 'jpg', 'jpeg', 'png', '');
if (!in_array(end(explode('.', strtolower($_FILES['filename']['name']))), $valid_extensions)) {
Header ("Location: errorupload.php?type=wrong");
} else {
Header ("Location: articleimages.php");
}
// rename the file(s)
if ($filename !='') {
//remove the eventual space
$filename = str_replace(' ', '_',$filename);
//get the timestamp
$now = date('Y-m-d-His');
//add the timestamp to the name
$filename = $now.$filename;
}
//upload the files
$folder = "myFolder/";
move_uploaded_file($_FILES['filename']['tmp_name'], $folder.$filename);
if (empty($filename)) {
$query = "UPDATE articleimages SET artid ='$artid', visible='$visible', sorder='$sorder'
WHERE image_id = '$image_id'";
} else {
$query = "UPDATE articleimages SET artid ='$artid', filename ='$filename', visible='$visible',
sorder='$sorder'
WHERE image_id = '$image_id'";
}
mysql_query($query);
mysql_close();
This is the part, where I actually update my table, so to perofrm the insert, you will need to change the script. myFolder will need 777 permissions so the image can be uploaded. Otherwise, you will have the record in the database, but the image will not be uploaded.