I'm hoping someone out there can help me out with this request. I am just starting out in PHP and expanding my coding knowledge, so please forgive me if any of the following seems ambiguous.
I am looking for a freely distributed script written in PHP that can do the following:
Allow any user on my website to upload various images to a private directory on my site, while allowing them to add comments in a comment box while uploading their images. I do not need these images to be displayed on any public webpage, but rather, have the pictures and comments that go along with them, readable by me only (I will later post these images on other pages of my choice with the comments).
I have looked at many of the image upload scripts here, but have not found one that has a seperate comments field for the image, and allows for private upload to myself only.
I am sure there is an easy fix for this, but as I said, I am new to PHP. Hopefully someone out there can understand what I am asking for.
In summation: a no-frills image upload script, allowing users to upload a picture and comments to their picture, to a private directory readable only by myself; and a script in which I can specify variables such as max image upload size, and file type.
Thanks in advance to anyone out there who can give me some help! -Phrozen44
Thanks for the reply. It is very much appreciated.
The hosting I will be using does indeed have MySQL, but only the 1 database at this time (I can later upgrade, which I will most likely do when I become more familiar with it).
As you most likely can guess, MySQL databases are another aspect of coding/databasing I am just starting to learn along with PHP (figured it would be wise as they both do go somewhat hand in hand).
I am a very quick learner however, and from what I have fiddled with already on PHP, I am confident I can start to move to bigger and better things, such as the script I am hoping to find! Any suggestions? Thanks again! -Phrozen44
This form uploads the specified image file and saves the information to the database.
To make the database part work, you need to fill in the values for the variables at the top of the page.
You will need to create a table in your MySql database with six columns.
Name the columns as follows:
name
size
width
height
mime_type
comments
After you get this form to work, let me know and I will create you a page where you can view the contents of the table and the image that was uploaded.
If you need more inputs in the form, then let me know what inputs you want.
I figure you will probably want some to identify who uploaded the image.
Any questions or problems, just ask.
PHP Code:
<?php // Insert the values for these variables // $server = ""; // MySql server *required* $username = ""; // MySQL username *required* $password = ""; // MySQL password *optional* $dbname = ""; // MySQL database name *required* $table = ""; // MySql table name *required* $path = ""; // Image folder name *optional* ////////////////////////////////////////// ?> <html> <head> <title></title> <style> table { background:#fafafa; color:#3399cc; } td { padding:20px; } .align_center { text-align:center; } .w125 { width:125px; padding-top:0px; padding-bottom:0px; } .red { color:#ff0000; font-size:20px; font-weight:bold; } .blue { color:#3399cc; font-size:20px; font-weight:bold; padding:5px; } .pl { padding-left:15px; padding-top:5px; padding-bottom:5px; } </style> </head> <body> <table border="1"> <?php $sw = 0; $types = array("image/bmp","image/gif","image/jpeg","image/png"); $file_attributes = getimagesize($_FILES['uploadedfile']['tmp_name']); $target = $path.basename($_FILES['uploadedfile']['name']) ? basename($_FILES['uploadedfile']['name']) : ""; $name = $target ? $target : " "; $size = $_FILES['uploadedfile']['size'] ? ceil($_FILES['uploadedfile']['size']/1000)." KB" : " "; $width = $file_attributes[0] ? $file_attributes[0] : " "; $height = $file_attributes[1] ? $file_attributes[1] : " "; $mime = $file_attributes["mime"] ? $file_attributes["mime"] : " "; echo "<tr><th class='blue' colspan='5'>File Attributes</th></tr> <tr><th>Name</th><th>Size</th><th>Width</th><th>Height</th><th>Mime Type</th></tr> <tr><td class='align_center w125'>$name</td><td class='align_center w125'>$size</td><td class='align_center w125'>$width</td><td class='align_center w125'>$height</td><td class='align_center w125'>$mime</td></tr>"; if(!in_array($file_attributes["mime"],$types)||$_FILES['uploadedfile']['size']>200000) { $msg = $target ? "<span class='red'>File <u>$target</u> is not allowed !!!</span>" : "<span class='red'>Please select a file.</span>"; } elseif(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target)) { $msg = "<span class='blue'>The file ". basename($_FILES['uploadedfile']['name'])." has been uploaded.</span>"; $sw = 1; } else { $msg = "<span class='red'>Sorry, there was a problem uploading your file.</span>"; } $comments = !empty($_POST["comments"]) ? $_POST["comments"] : "No comments entered."; echo "<tr><th class='blue' colspan='5'>Comments</th></tr> <tr><td class='pl' colspan='5'>$comments</td></tr> <tr><td class='align_center' colspan='5'>$msg</td></tr>"; ?> <tr> <td class="align_center" colspan="5"> <form enctype="multipart/form-data" action="#" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="300000" /> Choose a file to upload: <input name="uploadedfile" type="file" value="" /> <p>Comments: <textarea name="comments" rows=10 cols=35><?php echo $_POST["comments"]; ?></textarea></p> <input type="submit" name="submit" value="Upload File" /> </form> </td> </tr> </table> <?php if($sw) { mysql_connect($server,$username,$password) or die("Could not connect to server ".mysql_error()); mysql_select_db($dbname) or die(mysql_error()); mysql_query("INSERT INTO $table VALUES ('$name','$size','$width','$height','$mime','$comments')") or die("Record cound not be inserted. ".mysql_error()); mysql_close(); } ?> </body> </html>
Thanks again for the detailed reply; much appreciated.
I have already created a table to the specs in which you told me to create (was there anything else specific I needed to set anything to?).
As I stated earlier, I'm new to this, so please excuse my confusion with some of these things.
I copy and pasted the code you posted into a notepad file, and saved it simply as upload.php. I do place this in my /public_html directory, correct? The page itself appears fine, and looks clean, but upon testing it, I continued to get the same error.
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpP27W7X' to '01AwcAXyX-yWkAAAABAAAAAAAAAAA_.jpg' in /home/nwophoto/public_html/upload.php on line 74
I am sure this error is as plain as day to you, but for me, I am not so sure what I am doing wrong haha.
Once again, I thank you for your help (and patience!) in helping me. Any further help you can give this poor newbie would be greatly appreciated! -Phrozen44
Well, it's been a long time coming, but I think I can finally say, that this actually works! The new code you had written up worked like a charm. After fiddling with the permissions and tweaking some of the things I was writing, I believe I finally got this thing working as it should! As I learn more, I will more than likely add new things to this (such as multiple image upload at once etc.), but for now, I believe this will do just nicely.
I can't thank you enough for your kindness and patience in helping me out. We need more people like yourself in the coding community!