简体   繁体   中英

PHP mysql Insert/Upload images to database/server naming issues

I'm building a form to allow users to upload images into a database, as part of an app for an eye doctor. My issue concerns the table containing information on these images (the shot table), the columns of this table are:

ID | Visit_ID | Patient_ID | Img_Filename | Shrunk_img_filename | Subdir | SubSubDir|

So the table is only storing info about the images. The image files are named with specific conventions. For example, 0000288964r.jpg is what a file name in the Img_filename column would look like. The file names in the next column, Shrunk_img_filename are the same except the r is replaced with an s and the format is .bmp . For example, 0000288964s.bmp . These numbers in the file names are derived from the ID column of this table, with a variable number of zeros added to the begining depending on how many digits the ID is.

Also, in my form, I've got a file upload input where the user selects the image to be uploaded. This is where my issue comes into the mix..

I want to use the same file names, like shown above, that are stored in my database as the file names for these images when they're uploaded to my server, but I don't to manually go through the database find the next available ID for an image and rename the images at the time of selecting them for upload.

I really don't know if this is possible, but I'd love to hear any ideas about how to make this work or any kind of workaround for this. Thanks. I'll include my code below (my insert statement is ridiculously long due to the 2 CASE Statements used to create my files names so I just included the jist of it)

<?php
include('db.php');
if (isset($_FILES['myfile']) && isset($_GET['id'])) 
{
    $filename = $_FILES['myfile']['name'];
    $filetype = $_FILES['myfile']['type'];
    $filesize = $_FILES['myfile']['size'];
    $filetmp = $_FILES['myfile']['tmp_name'];
    $fileerror = $_FILES['myfile']['error'];
    $id = $_GET['id'];

if ( $fileerror > 0){
die("Error Uploading File Code $fileerror.");
}
else
{
move_uploaded_file($filetmp,"C:/wamp/www/vixer/images/".$filename);
echo "Upload Complete!";
}

$sqlvisit="INSERT INTO shot (VISIT_ID, PATIENT_ID, IMG_FILENAME, SHRUNK_IMG_FILENAME, SUBDIR, SUBSUBDIR, IMG_FILE_FORMAT, EYE) SELECT (SELECT max(ID) from visit where visit.patient_id =" . $id ."), " . $id . ", (SELECT CASE blah blah), (SELECT CASE blah blah), '9', '1', '124', '0' FROM SHOT WHERE shot.patient_id = " . $id . "";
mysql_query($sqlvisit);
}
?>

<form id="form3" action="" method="post" enctype="multipart/form-data" style="display:none;width:300px;background-color: #FFFFFF;"> 
Upload Image(s)..<input type="file" name="myfile"><br />
<input type="submit">
</form>

I think I basically did what prodigitalson suggested...

I include() a call to a php file that inserts a new shot(same query as in my question)

Then I include() another call to another php file that selects this new row I just inserted and assigns each column of the result row into variables and then into session variables

Then the session variables are used in my script that uploads the image to my server with the image filename and also creates a thumbnail image based on my shrunk image filename .

The easiest way to do this would be to just have 2 separate directories Images/R and Images/S. Set the shrunk image to save in the relevant location. You can then set the file path according to whether you want regular size or smaller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM