Jump to content

[SOLVED] Image upload - determine folder


waynew

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/178137-solved-image-upload-determine-folder/
Share on other sites

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.

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.

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;
   }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.