...

View Full Version : upload path to database



saunders1989
01-12-2010, 08:17 PM
Hi, i am struggling to get this part before i tackle the next part. i have a upload form i have created (very simple just a browse and a upload button) when you click upload this code is run (shown below) but that stores the file in the database as a file using blob but what i would really like to do is upload the images to a this folder (http://localhost/Blean_Photos/images) and have the path stored in the database. i havent a clue on how to change my code i have now to do that. the next part would be to display the images but i would like to tackle one part at a time

thank you to anyone who could help


<?php
// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
// Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0) {
// Connect to the database
$dbLink = new mysqli('localhost', 'root', '', 'gallery');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}

// Gather all required data
$name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
$mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
$data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']));
$size = intval($_FILES['uploaded_file']['size']);

// Create the SQL query
$query = "
INSERT INTO `images` (
`name`, `mime`, `size`, `data`, `created`
)
VALUES (
'{$name}', '{$mime}', {$size}, '{$data}', NOW()
)";

// Execute the query
$result = $dbLink->query($query);

// Check if it was successfull
if($result) {
echo 'Success! Your file was successfully added!';
}
else {
echo 'Error! Failed to insert the file'
. "<pre>{$dbLink->error}</pre>";
}
}
else {
echo 'An error accured while the file was being uploaded. '
. 'Error code: '. intval($_FILES['uploaded_file']['error']);
}

// Close the mysql connection
$dbLink->close();
}
else {
echo 'Error! A file was not sent!';
}

// Echo a link back to the main page
echo '<p>Click <a href="member-index.php">here</a> to go back</p>';
?>

shadowmaniac
01-12-2010, 08:42 PM
Refer to this: php file upload (http://www.tizag.com/phpT/fileupload.php)
Basically, most of the code you provided above will become redundant



<?php
// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
// Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0) {

// @@@@ Edits Start here @@@@
// Move the file to the path of your choice; follow example in the link provided earlier
// now connect to DB and store the $target_path (from linked example) into the DB
// simply modify your existing code to perform this task
// note that you may have to modify the DB
// @@@@ Edits end here @@@@

}
else {
echo 'Error! A file was not sent!';
}

// Echo a link back to the main page
echo '<p>Click <a href="member-index.php">here</a> to go back</p>';
?>


To retrieve the image path, connect to DB, find the image you're looking for (hopefully, you have a unique identifier that segregates images from each other) and then display it using the returned result.

saunders1989
01-12-2010, 11:04 PM
this is what i tried changing it to but i am not very good at php could you explain where ive gone wrong please. also when you say i have a unique identifier could you elaborate on that please.

thanks


if(isset($_FILES['uploaded_file'])) {
// Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0) {

$target_path = "http://localhost/Blean_Photos/images/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";

$dbLink = new mysqli('localhost', 'root', '', 'gallery');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());


$query = "INSERT INTO `images` (`name`, `mime`, `size`, `data`, `created`)
VALUES ('{$name}', '{$mime}', {$size}, '{$data}', NOW())";



}

else {
echo 'Error! A file was not sent!';
}

// Echo a link back to the main page
echo '<p>Click <a href="member-index.php">here</a> to go back</p>';
?>

shadowmaniac
01-13-2010, 01:26 AM
See comments within code:

if(isset($_FILES['uploaded_file'])) {
// Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0) {


// @@@@@@@@@@@@@@
// Path is wrong. You have to specify the path of the folder on the web server
// Typically, it'd be something akin to "var/www/Blean_Photos/images"
// Also make sure the folder exists and that you have write permissions for it
$target_path = "http://localhost/Blean_Photos/images/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
// @@@@@@@@@@@@@@
// this echo is not really necessary
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";

$dbLink = new mysqli('localhost', 'root', '', 'gallery');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
} // You missed this closing curly brace here


// @@@@@@@@@@@@@
// To keep things simple, we'll keep your DB structure the same except for 1 thing:
// add a field of type 'varchar' with 100 or so characters, in your 'images' table.
// Name it something like "image_path".
// I copied over your "old" code but note that I got rid of "data" (which was your image)
// and added $image_path variable instead

// Gather all required data
$name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
$mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
$size = intval($_FILES['uploaded_file']['size']);
$image_path = $dbLink->real_escape_string($target_path);

// @@@@@@
// Note changes to your query as well
// Make sure you added the "image_path" field to the "images" table!!
$query = "INSERT INTO `images` (`name`, `mime`, `size`, `data`, `created`, `image_path`)
VALUES ('{$name}', '{$mime}', {$size}, '', NOW(), '{$image_path}')";



}

else {
echo 'Error! A file was not sent!';
}

// Echo a link back to the main page
echo '<p>Click <a href="member-index.php">here</a> to go back</p>';
?>
That should do the trick. Note that code is untested.




also when you say i have a unique identifier could you elaborate on that please.

Something like an SSN, Student ID, ... that allows you to identify uploaded images from each other. This will make things easier when you try to retrieve image paths from the DB.

saunders1989
01-13-2010, 10:24 AM
thanks for that code theres a slight problem i cant understand how to fix it. im getting this error 'Warning: move_uploaded_file(http://localhost/Blean_Photos/images/invalid carriage.jpg) [function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections in C:\wamp\www\Blean_Photos\add_file.php on line 25

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\php5307.tmp' to 'http://localhost/Blean_Photos/images/invalid carriage.jpg' in C:\wamp\www\Blean_Photos\add_file.php on line 25' any ideas what that could be?

for the id i have a incrementing id at the moment but i will also be having radio buttons for certain galleries and depending on which one is selected a id will be givin and uploaded to the database aswell. is that possible? if that is i would be able to use a for loop that ++ til it reaches the end and display all the images that have the id=1 or id=2

thanks again youve been soo much help

shadowmaniac
01-13-2010, 04:17 PM
// @@@@@@@@@@@@@@
// Path is wrong. You have to specify the path of the folder on the web server
// Typically, it'd be something akin to "var/www/Blean_Photos/images"
// Also make sure the folder exists and that you have write permissions for it
$target_path = "http://localhost/Blean_Photos/images/";

See bolded. I didn't fix your target path in the code above as I didn't know what the structure of your web server looked like.
It should be something like those shown below or a variation of them:


$target_path = "images/";

OR


$target_path = "c:/wamp/www/Blean_Photos/images/";





for the id i have a incrementing id at the moment but i will also be having radio buttons for certain galleries and depending on which one is selected a id will be givin and uploaded to the database aswell. is that possible? if that is i would be able to use a for loop that ++ til it reaches the end and display all the images that have the id=1 or id=2

Yes it's possible. No, you don't really need a for loop as long as you have the appropriate SQL query, a while loop will do the trick.
Look up some tutorials on PHP/MySQL pertaining to selecting data from the DB and printing them out. That should be all you need.

saunders1989
01-13-2010, 05:19 PM
okay cool thanks for that. its all uploading nicely and it gets picked up in my folder and writes to my database. but for some reason it wont let me upload images above 2mb. i have no idea why. i havent got any restrictions as i can see even on my form there is not a 'MAX_SIZE' so i dont know why it wont let me do it. any ideas?

Thanks alot for your help!

shadowmaniac
01-13-2010, 05:38 PM
okay cool thanks for that. its all uploading nicely and it gets picked up in my folder and writes to my database. but for some reason it wont let me upload images above 2mb. i have no idea why. i havent got any restrictions as i can see even on my form there is not a 'MAX_SIZE' so i dont know why it wont let me do it. any ideas?

Thanks alot for your help!

Modify your php.ini file (http://www.wampserver.com/phorum/read.php?2,36154)

saunders1989
01-13-2010, 06:04 PM
ive changed it to this:

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 5M

but it doesnt seem to work either. i also have 2 php.ini files. is there any other files i should change?

shadowmaniac
01-13-2010, 07:09 PM
Refer to the link I posted. You might have to increase execution time, ... as well.
As for the php.ini, since you're using Wamp, on the windows taskbar, locate the wamp icon, left click on it, go to php and click on php.ini. That's the php.ini file you need to modify.

Do remember to restart wamp afterwards!

saunders1989
01-13-2010, 07:33 PM
it works now. thank you soo much for you help!!!

kerndog
02-03-2010, 12:01 AM
Hi Saunders,

Was wondering if you have moved onto your next part of project. Displaying images. I am a newbie to PHP and found this thread quite helpful



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum