Solved

Image uploaded but unable to view

Posted on 2011-09-07
11
221 Views
Last Modified: 2012-05-12
I have an image upload script which seems to be working fine:

		// Process new images. Resize to thumbnail and gallery.
		$path = $picpath . $_POST['gallery'] . '/';
		if( !is_dir($path) ) mkdir($path);
		
		for( $i=1; $i<=$upload_at_once; $i++ ) {
			if( empty($_POST['title'.$i]) ) continue;
			if( $_FILES['photo'.$i]['error'] > 0 ) trigger_error('#'. __LINE__ .'/'. $i .' - There was an error processing your file.',E_USER_ERROR);
			if( !$_FILES['photo'.$i]['size'] > 0 ) trigger_error('#'. __LINE__ .'/'. $i .' - There was an error processing your file.',E_USER_ERROR);
			
			list($name,$ext) = explode('.',$_FILES['photo'.$i]['name']);
			$name = substr($name,0,25);
			$name = $name . microtime(TRUE);
			$name = preg_replace('/[^a-zA-Z0-9_-]/','',$name);
			
			$galfull = $path . $name . '.' . $ext;
			$thfull = $path . $name . '_th.' . $ext;
			
			$temp = $path . 'temporary.' . $ext;
			if( !move_uploaded_file($_FILES['photo'.$i]['tmp_name'],$temp) ) trigger_error('#'. __LINE__ .'/'. $i .' - There was an error processing your file.',E_USER_ERROR);
			if( !imageResize($temp,$thfull,$thw,$thh,TRUE) ) trigger_error('#'. __LINE__ .'/'. $i .' - There was an error processing your file into a thumbnail.',E_USER_ERROR);
			if( !imageResize($temp,$galfull,$galw,$galh,TRUE) ) trigger_error('#'. __LINE__ .'/'. $i .' - There was an error processing your file into gallery size.',E_USER_ERROR);
			$sql = sprintf("INSERT INTO home_images
								(image_path,image_file,image_ext,image_title,image_caption,image_date,image_group,image_photographer)
								VALUES
								('images/home/%s/','%s','%s','%s','%s',NOW(),'%s','%s')",
								$db->real_escape_string($_POST['gallery']),
								$db->real_escape_string($name),
								$db->real_escape_string($ext),
								$db->real_escape_string($_POST['title'.$i]),
								$db->real_escape_string($_POST['caption'.$i]),
								$db->real_escape_string($_POST['gallery']),
								$db->real_escape_string($_POST['photographer'.$i])
								);
			if( !$db->query($sql) ) trigger_error('#'. __LINE__ .'/'. $i .' - There was an error adding to the database.',E_USER_ERROR);
			unlink($temp);
		}
		$content .= 'Successfully added new images.';

Open in new window

This is included by my loading mechanism and the content placed where it goes within the template. I then have an image viewing script, which is working for most pictures:
<?php

$image = ISSET($_GET['image']) ? $_GET['image'] : FALSE;
if( !$image ) die('Error #501:<br />There was a problem loading the image.'. var_dump($_GET));

/**
 * Print the page
 */

$sql = 'SELECT * FROM home_images WHERE id_image='. $image;
$result = $db->query($sql);
if( !$result ) die('MySQL Error: ('. mysqli_errno() .') '. mysqli_error .'<br />Query: '. $sql);
if( !$result->num_rows > 0 ) die('Error loading the selected image, please try again later.');
$info = $result->fetch_assoc();

$content .= '
<div id="main">
    <div style="height:5px;">&nbsp;</div>
    <center><a href="javascript:void()" onclick="history.go(-1);return false;">Back to previous page</a></center>
    <img src="'. sprintf('%sresources/%s%s.%s',$siteurl,$info['image_path'],$info['image_file'],$info['image_ext']) .'" style="padding: 10px 10px;" />
    <br />
    <h4>'. $info['image_title'] .'</h4>
    <p>'. $info['image_caption'] .'</p>
    ';
if( !empty($info['image_photographer']) ) $content .= '<p class="note">Photos provided by '. $info['image_photographer'] .'</p>';
$content .= '</div>
';
$sql = 'UPDATE home_images SET image_views = image_views+1 WHERE id_image='. $image;
$db->query($sql);

Open in new window


However I just used this script to upload 5 new pictures and they are not showing either in thumbnail or the view file. Also when I try to access these files directly via the full url http://www.domain.com/resources/images/home/groupname/imagename.jpg i get a 404 not found error. When I log into my FTP program I verified that these images are exactly where they are supposed to be.

Any idea why I can't seem to access these newly uploaded images? As I said its working fine for images that have been for there for a while, however the new images I just uploaded aren't showing, even tho they are located where the src tag says they are, as verified by the file system.
0
Comment
Question by:Derokorian
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 8

Expert Comment

by:pvlier
Comment Utility
Might be an .htaccess rewriterule that redirects requests for files with particular names?
Second, did you check if the case is correct? Unix servers are casesensitive about filesnames...
0
 
LVL 10

Author Comment

by:Derokorian
Comment Utility
To the second point: Yes I check the case of the filename, file path and the extension they are matching for both the thumbnail and full view. Which is very confusing to me.

To the first: I don't think my rewrites would be affecting anything, here is my file if you see an error:
RewriteEngine ON
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule (.+) $1 [L]

#page specific rewriting
RewriteRule ^waiting/(.*)/(.*)$ index.php?action=waiting&kid=$1&name=$2 [L]
RewriteRule ^waiting/(.*)$ index.php?action=waiting&page=$1 [L]
RewriteRule ^photos/(.*)$ index.php?action=photos&group=$1 [L]
RewriteRule ^view/(.*)$ index.php?action=view&image=$1 [L]
RewriteRule ^story/(.*)$ index.php?action=story&news=$1 [L]

#DTCO Rewrites
RewriteRule ^(table_info|auction_info|auction_rem|chef_info)/(.*)$ index.php?action=$1&id=$2 [L]

#general rewrite
RewriteRule ^(.*)$ index.php?action=$1 [L]

Open in new window

0
 
LVL 10

Author Comment

by:Derokorian
Comment Utility
Further investigation provides more confusion. If i use the wrong case to try to access the image directly via the address bar, instead of getting a 404 error, i get redirected to the home page (as would be expected). So that tells me that the rewrite sees that the file is there, and the server just isn't giving it to me?
0
 
LVL 10

Assisted Solution

by:Derokorian
Derokorian earned 0 total points
Comment Utility
It seems when I copied the images to their destinations they weren't given proper permissions.

Can anyone, looking at my code, tell me how to modify it so that the images created are given proper privileges? I need them to be web visible (obviously), its an apache server running on linux (not sure what distro). Any help would be greatly appreciated!
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
>>Also when I try to access these files directly via the full url http://www.domain.com/resources/images/home/groupname/imagename.jpg i get a 404 not found error.

Try comment out line 35:
                  //unlink($temp);
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 10

Accepted Solution

by:
Derokorian earned 0 total points
Comment Utility
Turns out I have to chmod the permissions on the new files, even tho these were inheritted before... oh well =D
0
 
LVL 10

Author Closing Comment

by:Derokorian
Comment Utility
Need to apply proper permissions to the files after creating them.
0
 
LVL 8

Expert Comment

by:pvlier
Comment Utility
Hi Dekorian, that's strange, because the files are uploaded using script that runs as the apache user (eg 'apache') so viewing them using apache would use the same user.... therefor the browser should be able to show them... also you said some files did work and some don't... very confusing... but glad you sorted your problem...
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
This is the code I use to "chmod" the uploaded files or issue an error message if the function fails.
if (!chmod ($my_new_file, 0755))
            {
                echo "<br/>chmod(0755) FAILED: fileperms() = ";
                echo substr(sprintf('%o', fileperms($my_new_file)), -4);
            }

Open in new window

0
 
LVL 10

Author Comment

by:Derokorian
Comment Utility
>>>also you said some files did work and some don't... very confusing... but glad you sorted your problem...

Well these files were uploaded when we were on our old server, so something is configured differently that is not defaulting the right permissions.

Thanks Ray, I prefer trigger error so that I can turn off those messages in production =D
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Yeah, I agree about trigger_error.  Sometimes I use output buffering to trap messages and email them to myself.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now