kurt.santo Posted May 13, 2008 Share Posted May 13, 2008 Working on script to upload images, which are going to be displayed on website later on. The following script works well, but I would like to actually change the name of the files as: user_id + number. The user id is stored in the session. It is important to make a connection between the images and appropriate user_id. Later on you will be able to select user and then see all his/her images on display page. If you have any other suggestions in how to make the association let me know... I am looking for the best option to achieve my objective. Code is: <?php include ('../user/includes/header.php');// if no first_name variable exists, redirect the userif (!isset($_SESSION['user_id'])) { // start defining the URL $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); // check for a trailing slash if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) { $url = substr ($url, 0, -1); // chop off the slash } // add the page $url .= '/login.php'; ob_end_clean(); // delete the buffer header("Location: $url"); exit(); // quit the script} else {$counter = 3; // Number of files to allow for.if (isset($_POST['submitted'])) { // Handle the form.require_once ('connect.php'); // connect to database for ($i = 0; $i < $counter; $i++) { // Handle each uploaded file. // Create index names to refer to the proper upload and description. $filename = 'upload' . $i; $description = 'description' . $i; // Check for a file. if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 4)) { // Check for a description (not required). if (!empty($_POST[$description])) { $d = "'" . escape_data($_POST[$description]) . "'"; } else { $d = 'NULL'; } // Add the record to the database. $query = "INSERT INTO uploads (file_name, file_size, file_type, description) VALUES ('{$_FILES[$filename]['name']}', {$_FILES[$filename]['size']}, '{$_FILES[$filename]['type']}', $d)"; $result = mysqli_query ($dbc, $query); if ($result) { // Return the upload_id from the database. $upload_id = mysqli_insert_id($dbc); // Move the file over. if (move_uploaded_file($_FILES[$filename]['tmp_name'], "../../uploads/$upload_id")) { echo '<p>File number ' . ($i + 1) . ' has been uploaded!</p>'; } else { // File could not be moved. echo '<p><font color="red">File number ' . ($i + 1) . ' could not be moved.</font></p>'; // Remove the record from the database. $query = "DELETE FROM uploads WHERE upload_id = $upload_id"; $result = mysqli_query ($dbc, $query); // Add more detailed error reporting, if desired. } } else { // If the query did not run OK. echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>'; // Print the query and invoke the mysql_error() function to debug. } } // End of if (isset($the_file)... } // End of FOR loop. mysqli_close($dbc); // Close the database connection. } // End of the main Submit conditional.}?><form enctype="multipart/form-data" action="add_file.php" method="post"> <fieldset><legend>Fill out the form to upload a file:</legend> <input type="hidden" name="MAX_FILE_SIZE" value="524288"> <?php // Create the inputs. for ($i = 0; $i < $counter; $i++) { echo '<p><b>File:</b> <input type="file" name="upload' . $i . '" /></p> <p><b>Description:</b> <textarea name="description' . $i . '" cols="40" rows="5"></textarea></p><br /> '; } ?> </fieldset> <input type="hidden" name="submitted" value="TRUE" /> <div align="center"><input type="submit" name="submit" value="Submit" /></div></form><?php include ('../user/includes/footer.php');?> Link to comment Share on other sites More sharing options...
justsomeguy Posted May 13, 2008 Share Posted May 13, 2008 You're going to want to save the filenames in the database, so you'll need one field in the user table for each filename, or a user ID field in the files table. You'll want to associate the files with users in the database, not just with filenames. You can use a loop like this to keep renaming the file until it's unique. $nr = 1;$fname = "../../uploads/{$upload_id}_{$nr}.jpg";while (file_exists($fname)){ $nr++; $fname = "../../uploads/{$upload_id}_{$nr}.jpg";} But once you figure out what the name is you need to store that in the database as well, so you need to rename and move the file before you add it to the database (so the ID returned from the database can't be part of the filename), or update the filename in the database after you move and rename it. Link to comment Share on other sites More sharing options...
kurt.santo Posted May 13, 2008 Author Share Posted May 13, 2008 I created the column for user_id in uploads and changed query in deleting the description (won't really need it) and using the user_id. $query = "INSERT INTO uploads (file_name, file_size, file_type, user_id) VALUES ('{$_FILES[$filename]['name']}', {$_FILES[$filename]['size']}, '{$_FILES[$filename]['type']}', '{$_SESSION['user_id']}')"; The insertion works well. Still, I have major problems in understanding the rest.I changed the form to simple html: <form enctype="multipart/form-data" action="add_file.php" method="post"> <fieldset><legend>Fill out the form to upload a file:</legend> <input type="hidden" name="MAX_FILE_SIZE" value="524288"> <p>File 1: <input type="file" name="upload1" /></p> <p>File 2: <input type="file" name="upload2" /></p> <p>File 3: <input type="file" name="upload3" /></p> <p>File 4: <input type="file" name="upload4" /></p> <p>File 5: <input type="file" name="upload5" /></p> <p>File 6: <input type="file" name="upload6" /></p> <p>File 7: <input type="file" name="upload7" /></p> <p>File 8: <input type="file" name="upload8" /></p> </fieldset> <input type="hidden" name="submitted" value="TRUE" /> <input type="submit" name="submit" value="Submit" /></form> I think it could actually make better sense to have the upload_id to uniquely identify a file, but add for later use a column upload_name or similar (which takes the value from name field in the relevant input tag - upload1 for example) and the user_id (which seems to work well). Would that make sense? In page where I am going to display images for selected company (user_id) there will be placeholders as such for upload1, upload2 etc...KurtKurt Link to comment Share on other sites More sharing options...
kurt.santo Posted May 14, 2008 Author Share Posted May 14, 2008 Think am getting there with the logic now...Cheers,Kurt Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.