waynew Posted October 18, 2009 Share Posted October 18, 2009 I have an upload script that may be accessed from different directories. Problem is, the script is made with "../images/uploads" as the folder place for uploaded images. Which means that if the script is accessed from the main home folder of the site, it won't work. I'd like a more dynamic solution. I seen somebody give an example of this a while back. I know that I'll have to use one of the $_SERVER variables. Just not sure which one exactly and how to go about doing it. Quote Link to comment https://forums.phpfreaks.com/topic/178137-solved-image-upload-determine-folder/ Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 Can't you just use absolute paths? Quote Link to comment https://forums.phpfreaks.com/topic/178137-solved-image-upload-determine-folder/#findComment-939234 Share on other sites More sharing options...
waynew Posted October 18, 2009 Author Share Posted October 18, 2009 The truth is, embarrassingly enough, I've never had to use them. I'm guessing that I use dirname(__FILE__) ? Quote Link to comment https://forums.phpfreaks.com/topic/178137-solved-image-upload-determine-folder/#findComment-939243 Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 If all your pages are routed through a single file, a simple hack would be to define a constant called something like ROOT_PATH that contains the path of your application's root on the file system. A better way would be to use some sort of auto loader in an OO environment. If you care about micro optimization, absolute paths are also faster because the system doesn't need to resolve the real path. That's only an issue in large enterprise applications though. Quote Link to comment https://forums.phpfreaks.com/topic/178137-solved-image-upload-determine-folder/#findComment-939248 Share on other sites More sharing options...
waynew Posted October 18, 2009 Author Share Posted October 18, 2009 Isn't it an issue that dirname(__FILE__) gives me backslashes...? Quote Link to comment https://forums.phpfreaks.com/topic/178137-solved-image-upload-determine-folder/#findComment-939254 Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 PHP doesn't care either way. You can replace them with forward slashes if you want, but you can actually use a mixture of forward and backward slashes in paths. Quote Link to comment https://forums.phpfreaks.com/topic/178137-solved-image-upload-determine-folder/#findComment-939260 Share on other sites More sharing options...
waynew Posted October 18, 2009 Author Share Posted October 18, 2009 Basically, the upload is handled by a class in a directory login/classes/ whereas the uploaded images get placed in images/uploads. The class will be used from both the home directory and the login/ directory. The first bit of the class looks like this: class ImageUpload{ protected $allowed_filetypes = array("image/jpeg"=>".jpeg","image/pjpeg"=>".jpeg"); protected $error = NULL; protected $directory = "../images/uploads"; protected $extension = NULL; protected $image = NULL; public $uploaded_images = array(); function __construct($error){ $this->error = $error; } What I want to be able to do is have the directory variable be dynamic. Quote Link to comment https://forums.phpfreaks.com/topic/178137-solved-image-upload-determine-folder/#findComment-939271 Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 You could make a setter method that sets the upload path. Quote Link to comment https://forums.phpfreaks.com/topic/178137-solved-image-upload-determine-folder/#findComment-939273 Share on other sites More sharing options...
waynew Posted October 18, 2009 Author Share Posted October 18, 2009 Thanks for the help! I had thought about that. Just wanted to know if I could figure it out. I think I solved it: <?php class ImageUpload{ protected $allowed_filetypes = array("image/jpeg"=>".jpeg","image/pjpeg"=>".jpeg"); protected $error = NULL; protected $directory = NULL; protected $extension = NULL; protected $image = NULL; public $uploaded_images = array(); function __construct($error){ $this->error = $error; $dir = dirname(__FILE__); $dir = str_replace("","/",$dir); //convert to forward slashes $dir = str_replace("login/classes","",$dir); //take away last two directories $dir .= "images/uploaded/".rand(1,7)."/"; //7 folders $this->directory = $dir; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/178137-solved-image-upload-determine-folder/#findComment-939279 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.