Quote:
Originally Posted by Publisher Bucks
Also, as a secondary question on the same subject, once the file is uploaded to the directory on the server, how would I go about submitting that url to a row in my SQL database and attaching it as an image to a specific recipe?
|
Depends on your logic and how you build your sql tables, but something along these lines...
else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newFileName)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
// add sql query here
INSERT INTO image_table (id, filename, recipe, ...)
VALUES ('', $newFileName, 'Fried titty', ...)
}
While you're at it, you might as well want to store the width and height of the image so you can add the width and height tags to your html output as well. After upload, you could also automatically create a thumbnail, as well as converting the image to webp (smaller in file size = good for seo).
Alternatively, you could move the uploaded image directly to a folder matching that recipe...
Could use the id of the recipe, or the name/slug (or hash) or whatever. Basically, creating a new folder for each new recipe in the database. Then in your recipe page, you can create a function to display images, which should then be located in that recipe folder with matching name.
I would consider such approach if you are planning on adding 1000s of recipes and thus, going to end up with one folder with a shit ton of image files.
During image upload, just check if the folder already exists or not, else go create it:
if (!is_dir('path/to/recipes/friedtitty')) mkdir(path/to/recipes/friedtitty', 0777, true);