PHP - Display Image From Uploaded Directory (php Mysql)
hi to all.Im currently displaying the images from my upload directory but the image does not display.please help thanks
Similar TutorialsHi Basically I've built a CMS where by my clients can upload a number of images. On the success page I want to display the images they uploaded by file name. The issue is the number of images can vary. They may upload 2 or 10 or 50 etc. So far I've come up with this: Code: [Select] // number of files $UN = 3; //I've set this to 3 for now, but this is passed from the upload page! // server directories and directory names $dir = '../properties'; $images = glob($dir.'/*.{jpg}', GLOB_BRACE); //formats to look for $num_of_files = $UN; //number of images to display from number of uploaded files foreach($images as $image) { $num_of_files--; $newest_mtime = 0; $image = 'BROKEN'; if ($handle = @opendir($dir)) { while (false !== ($file = readdir($handle))) { if (($file != '.') && ($file != '..')) { $mtime = filemtime("$dir/$file"); if ($mtime > $newest_mtime) { $newest_mtime = $mtime; $image = "$file"; } } } } if($num_of_files > -1) //this made me laugh when I wrote it echo $trimmed = ltrim($image, "../properties").'<br />'; //display images else break; } Without this piece of code: Code: [Select] $newest_mtime = 0; $image = 'BROKEN'; if ($handle = @opendir($dir)) { while (false !== ($file = readdir($handle))) { if (($file != '.') && ($file != '..')) { $mtime = filemtime("$dir/$file"); if ($mtime > $newest_mtime) { $newest_mtime = $mtime; $image = "$file"; } } } } It shows the first 3 files alphabetically. I want to view the last number of images added. With the above code it simply shows the last image added 3 times! So I need to get the time each image was added and then order by the newest added and limit to the number of images uploaded. Any suggestions please? Kindest regards Glynn hi, Is it possible to display an image that is above the root directory using php? for example if i have an image in /home/user/uploads/test.jpg and lets say my root directory for my site is /home/user/public_html/ is it possible to display test.jpg on /home/user/public_html/index.html ? i am trying to create a secure file upload script and i think not having the file be directly accessed by the public is one of the main steps. Code: [Select] <?php $path = "../assets/tattoos/"; $path2 = "../assets/tattoos-thumbs/"; if(isset($_POST['file']) && is_array($_POST['file'])) { foreach($_POST['file'] as $file) { unlink($path. "/" . $file) or die("Failed to delete file"); unlink($path2. "/" . $file) or die("<meta http-equiv=\"refresh\" content=\"0; url=index.php\" />"); } } ?> <form name="form1" method="post"> <?php $imageDir = "../assets/tattoos/"; $dir_handle = @opendir($path) or die("Unable to open folder"); while (false !== ($file = readdir($dir_handle))) { if($file == "index.php") continue; if($file == ".") continue; if($file == "..") continue; echo "<input type='CHECKBOX' name='file[]' value='$file'>"; echo "<img src='../assets/tattoos/$file' style='height:auto;width:8%;' alt='$file'>"; } closedir($dir_handle); ?> <input type="submit" name="Delete" value="Delete"> </form> I have this code, which calls an image directory and adds a checkbox next to it, you check the boxes you wish, hit delete and the pictures are removed from the server. I need them to display in a grid view, like 4 columns by X amount of rows. Any help would be greatly appreciated. After the file gets uploaded, I intend the file to become renamed, and then moved to its destination folder. Though to move the file, I need the newly renamed file name and also its location, which is the temporary folder, and the latter describes my question. This is the script as follows: Code: [Select] $avatar_tmp = $_FILES['avatar_upload']['tmp_name']; // Rename the file into a more usable file name $new_file_name = $user_name . '_' . rand(111111, 999999) . '.jpg'; rename($avatar_tmp, $new_file_name); // Move the uploaded file on the disk to its folder // The directory of the temporary folder is needed in front of the new_file_name variable move_uploaded_file ($new_file_name, $target); The directory of the temporary folder is needed in front of the new_file_name variable and my question is, is there any function, to get the directory of the temporary folder for the uploaded file? So I can insert it in front of the new_file_name variable and move it out of the folder to its destination folder? My photo files are not being displayed in my table? They get sent to the mySQL database, then the server and it does grab all the other variables in the table and displays them, but the .jpg's are not shown, instead theres just the file name?? Code: [Select] <?php error_reporting(E_ALL); ini_set("display_errors", 1); echo '<pre>' . print_r($_FILES, true) . '</pre>'; //This is the directory where images will be saved $target = "/home/users/web/b109/ipg.removalspacecom/images/COMPANIES"; $target = $target . basename( $_FILES['upload']['name']); //This gets all the other information from the form $company_name=$_POST['company_name']; $basicpackage_description=$_POST['basicpackage_description']; $location=$_POST['location']; $postcode=$_POST['postcode']; $upload=($_FILES['upload']['name']); // Connects to your Database mysql_connect("server****", "username***", "password****") or die(mysql_error()) ; mysql_select_db("DB") or die(mysql_error()) ; //Writes the information to the database mysql_query("INSERT INTO `Companies` (company_name, basicpackage_description, location, postcode, upload) VALUES ('$company_name', '$basicpackage_description', '$location', '$postcode', '$upload')") ; echo mysql_error(); //Writes the photo to the server if(move_uploaded_file($_FILES['upload']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ". basename( $_FILES['upload']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> "upload" is the variable that isnt displaying in my table how i want it to? Have you guys any ideas how to get it displayed correctly? Hi everyone!! I have looked into how the upload script works and this is what i have: Code: [Select] <?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> Which is un-tested at the moment, but let's just say for talking sake it worked 100% what elements of this script would i be looking at to display the files uploaded on to another page, in my case my homepage? ive found as to yet, that the uploads have to be stored on a file somewhere on my server, which i've set up. But i thought it would be just as easy to have a field in my table named upload and display it within the table next to the other results? instead i just get whatever the file name is named.jpg. Any help in looking towards the answer? many thanks in advance guys! Code: [Select] $thephoto = $row['thePHOTO']; in mysql thephoto is BLOB file type and stores an image i want to display the image as an avatar, in a while loop. Code: [Select] while($row = mysql_fetch_assoc($query4)) { $id = $row['ID']; $thename = $row['theNAME']; $thephoto = $row['thePHOTO']; } how do i make blob readable and then display as an image? Hi, I am having an issue with displaying an image that is uploaded in a mysql database. The table format is ----------------------------------------------------------- | id | name | mime | size | data | ----------------------------------------------------------- | 1 | test | image/jpeg| 74857 | BLOB | The data has uploaded fine. I then have the following script on my 'displayimage.php?imgid=1' page... Code: [Select] $id = $_GET['imgid']; $result = mysql_query("SELECT * FROM images WHERE id = '$id'"); while($row = mysql_fetch_array($result)) $size = $row['size']; $mime = $row['mime']; $name = $row['name']; { header("Content-length: $size"); header("Content-type: $mime"); header("Content-Disposition: attachment; filename=$name"); echo $row['data']; However everytime i try to open this page, Firefox or Internet Explorer just ask's me to try and download the file as a php file. Any ideas what is going wrong? Thanks! Matt Hi guys, I have a problem. I need to create a page that has a web form to upload an image to a mySQL database and place it in a blob field. Then I need to be able to query the database later to display the image on the site. I've looked around but I just haven't found any examples that can help me. Does anyone know of any good example or can anyone please give me an example? I have nothing so far, just the html web form and database. this is my 3 files which is i am using to show data of customer but i am not able to see them their logo image please see them & help me guys i dont know whats wrong in this. these files i uploaded please find attachment to see them. thnx in advanced [attachment deleted by admin] Hey everyone, I posted something similar to this before and thought I had received an answer, but for whatever reason, the script I wrote had inconsistent functionality. It would work sometimes, but not others. At any rate I had to completely revamp the script. Now I'm having the same initial problem with this new script. Basically, what I'm trying to do is get my PHP script to upload a file, insert form info into the MySQL Database Table, and insert the name of the uploaded file into the MySQL Database table as well. I've been able to get everything to work except inserting the name of the uploaded file into the MySQL Database table. I'll show you what I have currently (a stripped down version anyway) then any suggestions you could give would be much appreciated! Please help! I've been working on this for the past month and keep running into issues: Code: [Select] <?php $target_path = "Images/"; $target_path = $target_path . basename($name = $_FILES['pic']['name']); $first_name=$_POST['first_name']; $last_name=$_POST['last_name']; $middle_init=$_POST['middle_init']; mysql_connect("localhost", "My_Username", "My_Password") or die(mysql_error()) ; mysql_select_db("My_Database") or die(mysql_error()) ; if (!$_POST['first_name'] | !$_POST['last_name'] | !$_POST['middle_init']) { header('Location:http://www.mysite.com/error.html'); die(); } $insert = "INSERT INTO image (first_name, last_name, middle_init, name) VALUES ('".$_POST['first_name']."', '".$_POST['last_name']."', '".$_POST['middle_init']."', '".$_POST['name']."')"; $add_member = mysql_query($insert); move_uploaded_file($_FILES['pic']['tmp_name'], $target_path) ?> <HTML> <HEAD> <TITLE>My Site</TITLE> HTML "Confirmation Script" continues on from there. Like I said before, everything works except the name of the uploaded file doesn't get inserted into the MySQL table. Oh...that specific field in the MySQL table is titled "name". Anyway, again, any help you could give would be awesome! Thanks! Hello, I'm having some frustrations right now. I have built an application form where an applicant inputs their info and then uploads their resume. Even though the resume gets uploaded to a directory on the server i would also like the file name of the resume to get inserted into the mysql database so that I can easily see which resume matches which applicant...if that makes sense. At any rate I'm able to get everything else to work, the applicant's info gets inserted into the database, the resume gets uploaded to the directory, i just can't seem to get the resume file name to also insert into the database. Most of the info i have found on this subject through googleing and the like has been a little over my head and/or a little more complex than what i'm looking for. Here is an extremely simplified version of what I have right now. I have excluded the 'connect to database' code since it's working fine and really doesn't have anything to do with this issue: Code: [Select] <?php $target_path = "docs/"; $target_path = $target_path . basename( $_FILES['resume']['name']); $first_name=$_POST['first_name']; $last_name=$_POST['last_name']; $middle_init=$_POST['middle_init']; $resume=$_POST['resume']; mysql_query("INSERT INTO `intern` VALUES ('$first_name', '$last_name', '$middle_init', '$resume')") ; if (move_uploaded_file($_FILES['resume']['tmp_name'], $target_path)); ?> HTML begins after that. At any rate, any info, help, etc. you could provide would be GREATLY appreciated! Thanks! Hi: I'm a newby regarding uploading files to MySQL, turning report output into an HREF, and getting MySQL data via a hyperlink. I have successfully uploaded, files to MySQL, I have also been able to display filename information as a hyperlink in report output, but when I click on the hyperlink, I get the following message format on a 404 page: The requested URL /current_dir_of_requesting_page/filename.filetype was not found on this server. My reporting page has teh following line of code in a report table to create the hyperlink: <td style="width:225px"><? echo "<a href=".$row['name'].">".$row['name']."</a>";?></td> Can anyone assist me with this? Hi. I have a script here that will let users upload an image to my website but I just can't figure out how to save the uploaded image as "upload/logo.png" so that it will replace the already existing "upload/logo.png". Help would be greatly appreciated. Code: [Select] <html> <body> <form action="" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> <?php if(isset($_POST['submit']) && !empty($_FILES["file"]["name"])) { $timestamp = time(); $target = "upload/"; $target = $target . basename($_FILES['uploaded']['name']) ; $ok=1; $allowed_types = array("image/gif","image/jpeg","image/pjpeg","image/png","image/bmp"); $allowed_extensions = array("gif","png","jpg","bmp"); if ($_FILES['file']['size'] > 350000) { $max_size = round(350000 / 1024); echo "Your file is too large. Maximum $max_size Kb is allowed. <br>"; $ok=0; } if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; $ok=0; } else { $path_parts = pathinfo(strtolower($_FILES["file"]["name"])); if(in_array($_FILES["file"]["type"],$allowed_types) && in_array($path_parts["extension"],$allowed_extensions)){ $filename = $timestamp."-".$_FILES["file"]["name"]; echo "Name: " . $filename . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; $path_parts = pathinfo($_FILES["file"]["name"]); echo "Extension: " . $path_parts["extension"] . "<br />"; echo "Size: " . round($_FILES["file"]["size"] / 1024) . " Kb<br />"; //echo "Stored in: " . $_FILES["file"]["tmp_name"]. " <br />"; } else { echo "Type " . $_FILES["file"]["type"] . " with extension " . $path_parts["extension"] . " not allowed <br />"; $ok=0; } } if($ok == 1){ @move_uploaded_file($_FILES["file"]["tmp_name"], $target . $filename); $file_location = $target . $filename; if(file_exists($file_location)){ echo "Uploaded to <a href='$file_location'>$filename</a> <br />"; } else { echo "There was a problem saving the file. <br />"; } } } else { echo "Select your file to upload."; } ?> Thanks! How can i display all the images in a certain directory and echo the image names. So if a new image is uploaded, it will still display without edited the gallery.php file. Cheers. Not sure if this is the right place to post this. I have PHP form that I use to upload a document, PDF or Word Doc, I would also like the form to create a thumbnail of the document when it is uploaded, is this possible? Hi everyone, I have a script below, which uplads an image, however, the image name always starts with a capital letter, I want all letters to be small, how to adjust this please, thank you
$target_dir = ""; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $target_file = "$get_current_user.jpg"; $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Check if image file is a actual image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo ""; /// was File is an image $uploadOk = 1; } else { echo "File is not an image<br>"; $uploadOk = 0; } } // Check file size if ($_FILES["fileToUpload"]["size"] > 10000000) { echo "Sorry, this image is too large, please resize using Paint<br>"; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "Only JPG, JPEG, PNG & GIF files are allowed<br>"; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "There was an error<br>"; } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "Uploaded successfully (You may need to clear Cache to see the new image)"; mysql_query("UPDATE user SET user_image = 'https://.../images/users/$get_current_user.jpg' WHERE user_name = '$get_current_user' "); } else { echo ""; /// was Select a suitable image, file not uploaded yet } }
Hello. I'm using an Amazon S3 class to uploaded to S3. I have 2 upload boxes - The first uploads once and the second needs to upload twice - 1 full size, 1 thumb. The issue i'm having is that the 2nd image (the thumb) seems to be failing, although if I don't save the first full sized image I am able to upload the thumb. So I think the issue is with using the temp file twice? This is my code: //retreive post variables $fileName = $randomString . "_" . $_FILES['theFile']['name']; $fileTempName = $_FILES['theFile']['tmp_name']; $fileName2 = $randomString . "_" . $_FILES['theFile2']['name']; $fileTempName2 = $_FILES['theFile2']['tmp_name']; //move the file if ($s3->putObjectFile($fileTempName, "containerhere", $fileName, S3::ACL_PUBLIC_READ)) { echo "<strong>Uploaded Image</strong>"; }else{ echo "<strong>Something went wrong while uploading your file... sorry.</strong>"; } //move the file if ($s3->putObjectFile($fileTempName2, "containerhere", $fileName2, S3::ACL_PUBLIC_READ)) { echo "<strong>Uploaded Image</strong>"; }else{ echo "<strong>Something went wrong while uploading your file... sorry.</strong>"; } include('simpleImage.php'); $image = new SimpleImage(); $image->load($_FILES['theFile2']['tmp_name']); $image->resizeToWidth(100); $image->save($_FILES['theFile2']['tmp_name']); $fileName3 = $randomString . "_" . $_FILES['theFile2']['name']; $fileTempName3 = $_FILES['theFile2']['tmp_name']; //move the file if ($s3->putObjectFile($fileTempName3, "containerhere", "thumbs/" . $fileName3, S3::ACL_PUBLIC_READ)) { echo "<strong>Uploaded Image</strong>"; }else{ echo "<strong>Something went wrong while uploading your file... sorry.</strong>"; } Can anyone offer any advice? Thanks. |