PHP - Displaying Image Regardless Of Extension
Hi everyone,
Sorry not sure if this is a php or html problem. Im using php and a html form to upload images to my site, i have made it so that jpg, jpeg, gif and png images can be uploaded. The problem im having is displaying the image. Code: [Select] <img src="images/<?php echo $name ?>.jpg" /> That works fine if a jpg was uploaded, but what if a png or a gif was uploaded? The $name is going to be unique, there will not be more than one image with the same name, so what do i have to do to display the image regardless of what the extension is? Thanks Similar TutorialsI'm trying to determine why when I select Upload, on the html page I see a message "My extension is:" only, instead of My extension is: webM" for example.Here's the code, I look forward to any assistance: <?php foreach (array('video', 'audio') as $type) { if (isset($_FILES["${type}-blob"])) { $fileName = $_POST["${type}-filename"]; $uploadDirectory = 'uploads/' . $fileName; // make sure that one can upload only allowed audio/video files $allowed = array( 'webm', 'wav', 'mp4', 'mov' ); $extension = pathinfo($filePath, PATHINFO_EXTENSION); die("My extension is: " . $extension); if (!$extension || empty($extension) || !in_array($extension, $allowed)) { echo 'Invalid file extension: '.$extension; return; } if (!move_uploaded_file($_FILES["${type}-blob"]["tmp_name"], $uploadDirectory)) { echo (" problem moving uploaded file"); } } } ?>
Hi folks, just upgrading one of my sites with a new script which i thought i had working, well it did at least on my local server. Its an image upload script. It fails at the get image extension part, which when echoed out is blank. Which is why its failing. Can anyone see what i have missed? if($_POST){ $image =$_FILES["image"]["name"]; $img = $_FILES['image']['tmp_name']; $size = filesize($_FILES["image"]["tmp_name"]); $maxsize= 400; if(isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH']>409600){ echo 'Upload FAILED, file is too large !'; exit(); } if ($size > $maxsize*1024){ echo "File too big"; exit(); } else { if(file_exists($existingimage)){ unlink($existingimage); } function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $extension = getExtension($image); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo "The extension is:" . $extension ." "; echo'Unknown Image extension'; exit(); } Ive ended the script here at the part where it fails to reduce the amount of uneeded code to sift through. It fails no matter what type of image is used to upload. Help me I am getting Notice: Undefined index: extension I am working on a PHP project that will allow a user to upload a handful of images, and then submit text to go along with it. To simplify the uploading process of a file, I restricted the type of file that can be uploaded (jpg) and the size (2.5mb). The problem is, the form is calling to a PHP script, and I have error logging built in to alert the user of why a file didn't upload, but since the PHP script is just being called, no errors are being displayed on the page. I guess unless I do a refresh of a few seconds to view any errors on the page.... What I would like to do is show a pop-up/error message notifying the user that the file type or size is incorrect and needs to be adjusted. Now part of the problem is, the file needs to be uploaded to the server first, and then everything can be checked, so I am really getting confused. If someone can point me in the right direction on what I could use to accomplish this, it would be greatly appreciated. Thanks -beemer I am using this code Code: [Select] <?php echo IMAGES_HEADER . "header_02.jpg"; ?> Instead of displaying the actual image on my site I am getting the path of the image. It is displaying "images/header/header_02.jpg" instead. Thank You in advance i have a db which store jpg image thru the upload prg code in php.But the image is not displayed properly in the <img> and also when i echo the blob data. how to correct this code.i am pasting the code below $result = mysql_query("select cover from Movies where movie_id=4"); $row = mysql_fetch_row($result); $data = base64_decode($row[0]); $im = imagecreatefromstring($row[0]); imagejpeg($im); header('Content-type: ' . $mime); // 'image/jpeg' for JPEG images echo $data; <img src=<?php echo $data;?>> I must be doing something incredibly daft, because I'm incredibly new at this. I have an image stored in a DB under a table called 'images' and I want to display it on my website but instead of that image I get the error: Warning: Cannot modify header information - headers already sent by (output started at /home/... This is how I'm trying to achieve it. Any ideas where I'm doing wrong? Thanks. Code: [Select] <?php $user="###"; $password="###"; $database="###"; $con = mysql_connect(localhost,$user,$password); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($database, $con); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>MySite</title> </head> <body> <div id="container"> <?php include("../navbar.php"); ?> <div id="left-content"></div> <div id="right-content"> <?php $item = $_GET['item']; $query = "SELECT * FROM main WHERE Ref='$item'"; $result = mysql_query($query) or die("Oops" .mysql_error()); $row = mysql_fetch_array($result,MYSQL_BOTH) or die("Oops" .mysql_error()); extract($row); $query2 = "SELECT image FROM main WHERE Ref='$item'"; // the result of the query $result2 = mysql_query($query2) or die("Invalid query: " . mysql_error()); header("Content-type: image/jpg"); echo mysql_result($result2, 0,'image'); echo "<p><strong>Name: </strong>".$FirstName." - ".$SecondName."</p>"; mysql_close($con); ?> </div> <br /> <?php include("../footer.php"); ?> </div> </div> </body> </html> MOD EDIT: [code] . . . [/code] BBCode tags added. Now I am trying to display the images from my table and I have almost got it working, except for one small thing --- it seems to be displaying all the records, but instead of displaying the right image for each record, it's displaying the same image across all the records. Can anyone tell me what I have done wrong? Code: [Select] this is the gallery <?php $dbhost = 'localhost'; $dbuser = 'webdes17_lizkula'; $dbpass = 'minimoon'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'webdes17_jewelry'; mysql_select_db($dbname); $all_records = "SELECT * FROM gallery"; $all_records_res = mysql_query($all_records); $image = mysql_result($all_records_res, 0, 'image'); while($nt=mysql_fetch_array($all_records_res)){ echo "<img src=http://www.webdesignsbyliz.com/wdbl_wordpress/wp-content/themes/twentyten_2/upload/$image>"; } ?> I'm guessing I have the $image variable in the wrong place, but when I tried placing it within the while statement, the page never loaded, and instead acted like it was loading forever. What am I doing wrong? Here is the link to the page so you can see what is happening: http://webdesignsbyliz.com/wdbl_wordpress/test-submit/ I just can't get anything right today! Now I am trying to have a list of images, and when the user clicks on the image, the next page will display the image along with other fields for that record. I am sending the id through the hyperlink to the next page, and I have echoed it to ensure it's comign through, but I cannot get anythign to display ont he next page. What am I doin wrong? Here is the link to the page. If you click on one of the images, you 'll see that the next page is empty: http://webdesignsbyliz.com/wdbl_wordpress/test-submit/ Here is my code: Code: [Select] this is the gallery <?php $dbhost = 'localhost'; $dbuser = 'user'; $dbpass = 'pass'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'jewelry'; mysql_select_db($dbname); $all_records = "SELECT * FROM gallery"; $all_records_res = mysql_query($all_records); $image = mysql_result($all_records_res, 0, 'image'); $id = mysql_result($all_records_res, 0, 'id'); while($nt=mysql_fetch_array($all_records_res)){ echo "<a href=http://webdesignsbyliz.com/wdbl_wordpress/test-display/?id=" .$nt['id']." ><img src=http://www.webdesignsbyliz.com/wdbl_wordpress/wp-content/themes/twentyten_2/upload/".$nt['image']." width=133 height=86></a>"; } ?> display page: Code: [Select] <?php $id = $_GET['id']; $dbhost = 'localhost'; $dbuser = 'user'; $dbpass = 'pass'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'jewelry'; mysql_select_db($dbname); $all_records = "SELECT * FROM gallery WHERE id = $_GET[id]"; $all_records_res = mysql_query($all_records); $image = mysql_result($all_records_res, 0, 'image'); $id = mysql_result($all_records_res, 0, 'id'); while($nt=mysql_fetch_array($all_records_res)){ echo "<img src=http://www.webdesignsbyliz.com/wdbl_wordpress/wp-content/themes/twentyten_2/upload/".$nt['image']." width=133 height=86></a>"; } ?> What an I doing wrong this time?? :-( Good day: Im trying to display an image from a folder and the image name is retrieved by a query. The query is getting the image name all right but the image is not displaying. This is the code im using for the image display Code: [Select] <?php $aid = 1; $connection = mysql_connect("localhost", "username", "password"); mysql_select_db("articles", $connection); $query="SELECT imagename, description FROM articles_description WHERE id='$aid'"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); ?> <table width ="1000" border="1" cellspacing="2" cellpadding="2"> </tr> <?php $j=0; while ($j < $num) { $f8=mysql_result($result,$i,"description"); $f9=mysql_result($result,$i,"imagename") ?> <tr> <td valign="top"> <img src="images/'.$f9.'; ?>" alt="" name="picture" width="100" height="100" border="1" /></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f8; ?></font></td> </tr> <tr> </tr> <?php $j++; } ?> Any help will be appreciated Hi, I am uploading an image to a folder and I need to display the uploaded image in a new page. This is my code for the upload: Code: [Select] <?php //define a maxim size for the uploaded images in Kb define ("MAX_SIZE","2000000"); //This function reads the extension of the file. It is used to determine if the // file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } //This variable is used as a flag. The value is initialized with 0 (meaning no // error found) //and it will be changed to 1 if an errro occures. //If the error occures the file will not be uploaded. $errors=0; //checks if the form has been submitted if(isset($_POST['upload'])) { //reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; //if it is not empty if ($image) { //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //if it is not a known extension, we will suppose it is an error and // will not upload the file, //otherwise we will do more tests if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { //print error message echo '<h1>Tipo de imagen no permitido.</h1>'; $errors=1; } else { //get the size of the image in bytes //$_FILES['image']['tmp_name'] is the temporary filename of the file //in which the uploaded file was stored on the server $size=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($size > MAX_SIZE*1024) { echo '<h1>La imagen es demasiado grande.</h1>'; $errors=1; } //we will give an unique name, for example the time in unix time format $image_name=time().'.'.$extension; //the new name will be containing the full path where will be stored (images //folder) $newname="banners/".$image_name; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Error al subir la imagen.</h1>'; $errors=1; }}} //If no errors registred, print the success message if(isset($_POST['Submit']) && !$errors) { echo "<h1>La imagen se ha cargado correctamente</h1>"; } $query = "INSERT INTO banner_rotator.t_banners (banner_path) ". "VALUES ('$newname')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); //echo "<br>Files uploaded<br>"; header("Location: PC_cropbanner.php"); }Sowhat I need to do is to display the uploaded image in PC_cropbanner.php. How can I do this? I've taken this eg from php manual site for displaying txt on image wonder why it is not working Code: [Select] <?php // Create a 300x150 image $im = imagecreatetruecolor(300, 150); $black = imagecolorallocate($im, 0, 0, 0); $white = imagecolorallocate($im, 255, 255, 255); // Set the background to be white imagefilledrectangle($im, 0, 0, 299, 299, $white); // Path to our font file $font = './arial.ttf'; // First we create our bounding box for the first text $bbox = imagettfbbox(10, 45, $font, 'Powered by PHP ' . phpversion()); // This is our cordinates for X and Y $x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) - 25; $y = $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) - 5; // Write it imagettftext($im, 10, 45, $x, $y, $black, $font, 'Powered by PHP ' . phpversion()); // Create the next bounding box for the second text $bbox = imagettfbbox(10, 45, $font, 'and Zend Engine ' . zend_version()); // Set the cordinates so its next to the first text $x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) + 10; $y = $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) - 5; // Write it imagettftext($im, 10, 45, $x, $y, $black, $font, 'and Zend Engine ' . zend_version()); // Output to browser header('Content-Type: image/png'); imagepng($im); imagedestroy($im); ?> Hi guys, I have followed a tutorial and made a members only area using sessions. The user can upload an image and which gets renamed as their username. I was hoping to display all the users images that are logged in. I know how to do it with a single image by just setting the img src as the session username but I don't know how I would display multiple images if more than one person were logged in. Is it even possible? I am having trouble with displaying an image from a url. In the following code if I echo out $test I get the url of the image. However, in the current code I am trying to display the image, but the only thing that gets displayed is a small broken image in the upper left.
$html = file_get_contents($website.$criteria); $dom = new DOMDocument; @$dom->loadHTML($html); $links = $dom->getElementsByTagName('img'); header('Content-Type: image/png'); foreach ($links as $link){ $test = $link->getAttribute('src'); echo file_get_contents($test); }image that gets displayed: http://imgur.com/epfF3wJ Hi everyone, i am just trying to learn php for a bit of fun really and started making a sort of 'facebook' website. I am having trouble however trying to display different users images, for example when trying to find a correct 'friend' only the image of the last result is being shown for all people with the same name... here is my code below, if anyone can help me out that would be great file 1 $count=1; while ($numids>=$count){ echo "<form method=\"post\" action=\"friendadded.php\">"; $frienduserid=$_SESSION["passedid[$ii]"]; $friendfirstname=$_SESSION["passedfirstname[$ff]"]; $friendlastname=$_SESSION["passedlastname[$ll]"]; $_SESSION['friendsuserpicid'] = $frienduserid; echo "<table width=\"700\" height=\"50\" border=\"1\" align=\"center\">"; echo "<tr>"; echo "<th></th>"; echo "<th>First Name</th>"; echo "<th>Last Name</th>"; echo "</tr>"; echo "<tr>"; echo "<td><center>"; echo "<img border=\'0\' src=\"frienduserpic.php\" width=\"80\" height=\"80\" align=\"middle\"/>"; echo "</center></td>"; echo "<td><center>"; echo $friendfirstname; echo "</center></td>"; echo "<td><center>"; echo $friendlastname; echo "</center></td>"; echo "</tr>"; echo "</table>"; echo "<center><input type=\"submit\" value=\"Add this friend\" name=\"Add Friend\"></center><br/>"; $ii=$ii+1; $ff=$ff+1; $ll=$ll+1; $count=$count+1; echo "</form>"; } file 2 session_start(); $passeduserid=$_SESSION['friendsuserpicid']; $timespost=$_SESSION['postednum']; $host= $username= $password= $db_name= $tbl_name= mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $query = mysql_query("SELECT * FROM $tbl_name WHERE picid='".$passeduserid."'"); $row = mysql_fetch_array($query); $content = $row['image']; header("Content-type: image/jpeg"); echo $content; Thanks in advance Hi all, I have this script below where I am trying to display a default image if an image can not be found. For some reason though it is not working. <?php foreach (glob('./aircraft/' . $rowX['reg'] . '[0-8].jpg') as $file) { if (file_exists($file)) { echo "<img src=\"" . $file . "\" /><br />"; } else { echo "><img src=\"aircraft/wrightflyer.jpg\" /><br />";} } ?> i have an image uploader... that people can link directly to an image. But say for example that image gets deleted. When someone tries to link to it i want it to display a custom imagedeleted.jpg how can i do this? I'm beginning with simple codes to understand how things work and work my way up with what I need. My upload form looks like this: <form name="upload about" action="display.php" enctype="multipart/form-data" method="post"> Header: <input type="text" name="header" /> Body: <input type="text" name="body" /> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> Image: <input type="file" name="pic" /> <input type="submit" /> </form> And my display page is just as simple. <?php echo $_POST["header"]; ?> <?php echo $_POST["body"]; ?> <?php echo $_FILES['pic']['name']; ?> The problem is, whenever I submit the data, everything is fine, except the image does not display and just posts the filename of the image I uploaded. According to my research setting the enctype to multipart/formdata should display the image, but it does not. Can anyone tell me what's wrong? the script succesfuly insert image to the database bt, i cant be able to display it on my pages, any help i will appreciate
Attached Files
saveimage.php 1.15KB
2 downloads
images_tbl.php 192bytes
3 downloads Hi guys, I have a page that lets you upload an image to a an ftp server. Now I am trying to create a page that will get those images and display them in a web page. I am trying to do this with php. Does anyone know if it can be done? Thanks guys, |