Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2006
    Posts
    84
    Thanks
    47
    Thanked 0 Times in 0 Posts

    Not parsing correctly

    Hello,

    I have this code

    Code:
    $hash = 'cache/photos/'.md5($c->PhotoUrl).'.jpg';
    
    		$cachetime = 5 * 60;
    
    		if (!file_exists($hash) || filemtime($hash) < time() - $cachetime) {
    
    			$ch = curl_init();
    
    			curl_setopt($ch, CURLOPT_URL, $c->PhotoUrl);
    
    			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
    
    			$fc = curl_exec($ch);
    
    			curl_close($ch);
    
    			$img = imagecreatefromstring($fc);
    
    			imagejpeg($img, $hash);
    
    		}
    I use this is an included file all over my site, the problem is that if I include it in any other page than is on the root of the domain, it states...

    Warning: imagejpeg() [function.imagejpeg]: Unable to open 'cache/photos/184016efbd9944047109be2735485cbb.jpg' for writing: No such file or directory in...

    I tried setting

    Code:
    $hash = 'cache/photos/'.md5($c->PhotoUrl).'.jpg';
    to

    Code:
    $hash = '/cache/photos/'.md5($c->PhotoUrl).'.jpg';
    so it would save it to the root, but this doesn't seem to work, I need to be able to write to the root with imagejpeg() no matter where this code is on the server and then be able to call the image path with a variable.

    Thanks.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This appears to simply be a path problem. The path cache/photos must be available as a subdirectory starting from the executing script location. Using /cache/photos will almost guarantee to fail since its highly improbable that a cache directory has been provided as a mount point from root. The 'root' as you describe is is different from root. Root is simply / on your filesystem, though your site root is more likely to be under /user/XXX/public_html as an example. I never recommend using absolute paths, but you may generate absolute from relative paths, which is my recommendation since PHP executes from the cwd of the executing script, and never from any inclusion script. This is done using dirname(__FILE__), but only applies if you plan to actually move against this script's cwd, and only has an actual effect if the script is included in another script located in a different directory. I do not recommend using _SERVER to retrieve the site root.
    As for what you have above, I don't know what you have to modify since I don't know your structure or how its used. Best thing to test first is to simply pump the file without the path to see where it writes it and confirm you can write it. That should give you some insight as to where you are relative to where you have to go.

  • Users who have thanked Fou-Lu for this post:

    jdblewitt (11-15-2011)

  • #3
    New Coder
    Join Date
    Sep 2006
    Posts
    84
    Thanks
    47
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    This appears to simply be a path problem. The path cache/photos must be available as a subdirectory starting from the executing script location. Using /cache/photos will almost guarantee to fail since its highly improbable that a cache directory has been provided as a mount point from root. The 'root' as you describe is is different from root. Root is simply / on your filesystem, though your site root is more likely to be under /user/XXX/public_html as an example. I never recommend using absolute paths, but you may generate absolute from relative paths, which is my recommendation since PHP executes from the cwd of the executing script, and never from any inclusion script. This is done using dirname(__FILE__), but only applies if you plan to actually move against this script's cwd, and only has an actual effect if the script is included in another script located in a different directory. I do not recommend using _SERVER to retrieve the site root.
    As for what you have above, I don't know what you have to modify since I don't know your structure or how its used. Best thing to test first is to simply pump the file without the path to see where it writes it and confirm you can write it. That should give you some insight as to where you are relative to where you have to go.
    Thanks so much, I figured it out using your logic....

    Code:
    $hash = '/cache/photos/'.md5($c->PhotoUrl).'.jpg';
    
    		$cachetime = 5 * 60;
    
    		if (!file_exists($hash) || filemtime($hash) < time() - $cachetime) {
    
    			$ch = curl_init();
    
    			curl_setopt($ch, CURLOPT_URL, $c->PhotoUrl);
    
    			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
    
    			$fc = curl_exec($ch);
    
    			curl_close($ch);
    
    			$img = imagecreatefromstring($fc);
    
    			imagejpeg($img, $_SERVER['DOCUMENT_ROOT'].$hash);
    
    		}
    I made it write the file using the absolute path, and then called on the variable $hash for the image source code.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Just be aware that you are now trapped in a web only environment. $_SERVER guarantees nothing when it comes to populating data, and DOCUMENT_ROOT is pretty much guaranteed to be empty in cli, so you cannot cron a job that uses it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •