Click to See Complete Forum and Search --> : uplaod image/store path in database


ashokvas
01-21-2005, 10:59 PM
I want to upload 20 images for each member and store the filepaths in a databse alongwith their other details.
I have tried out some image upload scripts but I am not able to insert the path in the table.Can anyone pls help.

phpnovice
01-22-2005, 09:52 AM
Originally posted by ashokvas
I am not able to insert the path in the table.Can anyone pls help.
Are you talking about the original path that the file had on the visitor's machine? ...or, the path under which you save the file on the server?

ashokvas
01-22-2005, 10:27 AM
I want to save it in a specific folder under any name (like a timestamp or any name that we chose to give it).

I hope I have been able to answer your question

phpnovice
01-22-2005, 07:13 PM
Well, the following is an example I got from the us2.php extended php manual (http://us2.php.net/get/php_manual_chm.zip/from/a/mirror).
(Sorry, I couldn't get this site to not break the IF statement in the wrong place.)
<?php
// In PHP earlier then 4.1.0, $HTTP_POST_FILES should be used instead of
// $_FILES. In PHP earlier then 4.0.3, use copy() and is_uploaded_file()
// instead of move_uploaded_file

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir. $_FILES['userfile']['name'];

print "<pre>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile)) {
print "File is valid, and was successfully uploaded. ";
print "Here's some more debugging info:\n";
print_r($_FILES);
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}
print "</pre>";

?>
This will move the uploaded file from the temp area to the path you specify. Do you already have code like this?

ashokvas
01-22-2005, 08:00 PM
Thanks.But I am having problem inserting the filename in the database.Also what to do for
1)I want to name the file with a new name,using a fixed name,like 'photo1' or 'photo2'....
2)If I dont like the photo uploaded to 'photo1' location earlier,then I should be able to upload another photo to that location and overwrite the earlier image file.

Thanks a ton!!

phpnovice
01-22-2005, 10:20 PM
Originally posted by ashokvas
I am having problem inserting the filename in the database.
Show the code you're attempting to use to insert the file name into the database.

ashokvas
01-23-2005, 09:56 PM
Here is the code

<?php
include ("access_user_class.php");

$page_protect = new Access_user;
// $page_protect->login_page = "login.php"; // change this only if your login is on another page
$page_protect->access_page(); // only set this this method to protect your page

if (isset($_GET['action']) && $_GET['action'] = "log_out") {
$page_protect->log_out(); // the method to log off
}
include ("upload_class.php"); //classes is the map where the class file is stored (one above the root)
include("mysql_connectinfo.inc");
$max_size = 1024*100; // the max. size for uploading
echo "Welcome ".$_SESSION['user']." !";
echo "Welcome ".$_SESSION['pw']." !";
$my_upload = new file_upload;

$my_upload->upload_dir = "images/"; // "files" is the folder for the uploaded files (you have to create this folder)
$my_upload->extensions = array(".png",".gif",".jpg", ".zip"); // specify the allowed extensions here
// $my_upload->extensions = "de"; // use this to switch the messages into an other language (translate first!!!)
$my_upload->max_length_filename = 100; // change this value to fit your field length in your database (standard 100)


if(isset($Submit)) {
$my_upload->the_temp_file = $_FILES['upload']['tmp_name'];
$my_upload->the_file = $_FILES['upload']['name'];
$my_upload->http_error = $_FILES['upload']['error'];
$my_upload->replace = "y"; // because only a checked checkboxes is true
$my_upload->rename_file = "true";
$my_upload->do_filename_check = (isset($_POST['check'])) ? $_POST['check'] : "n"; // use this boolean to check for a valid filename
$my_upload->upload();
// use the following if clause to do something if the upload is true
if ($my_upload->upload()) { $src_filename="images/".$this->file_copy ;
$sql = "UPDATE users SET foto1 = '$src_filename' WHERE pw = $_SESSION['pw'] AND login=$_SESSION['user']";

$result = @mysql_query( $sql ) or die("Error inserting record: " . mysql_error());


}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Upload example</title>
</head>

<body><img src="<?php echo $src_filename;?>"/>
<h3>File upload script:</h3>
<p>Max. filesize = <?php echo $max_size; ?> bytes.</p>
<form name="form1" enctype="multipart/form-data" method="post" action="<?php echo $PHP_SELF; ?>">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_size; ?>">

<input type="file" name="upload" size="30"><br>
Replace ? <input type="checkbox" name="replace" value="y"><br>
Validate filename ?
<input name="check" type="checkbox" value="y" checked>
<input type="submit" name="Submit" value="Submit">
</form>
<p><?php echo $my_upload->show_error_string(); ?></p>
</body>
</html>


I am using two classes here-one is 'access_user' which is for user login and authentication.Other is 'upload class' for uplaoding images.
The image gets uploaded but no update of database.

Thanks once again

phpnovice
01-23-2005, 10:11 PM
This statement:

$sql = "UPDATE users SET foto1 = '$src_filename' WHERE pw = $_SESSION['pw'] AND login=$_SESSION['user']";

needs to at least be changed to this:

$sql = "UPDATE users SET foto1 = '{$src_filename}' WHERE pw = '{$_SESSION['pw']}' AND login = '{$_SESSION['user']}';";