PHP upload image to one directory from multiple pages - php

I have a script that is uploading images to my site, it works locally (haven't even tested it on the web server yet) but the problem is that I can't figure out how to get it to upload the images to a central location no matter where the script is run from.
For example my site structure looks like this:
/ROOT/
/IMAGES/
/USER/
upload.php
/IMAGES/
/ADS/
upload.php
/IMAGES/
/COUPONS/
upload.php
/IMAGES/
Right now the different upload.php files inherit from another php file that has the uploading script.
Inside the script the line that sets the upload path looks like this $newname = "images/".$image_name;.
That line is why I have an "images" directory under User and one under ADs and one under Coupons. What I want is to be able to have my script upload all images to the /IMAGES/ directory under the /ROOT/ directory, but I can only figure out how to make the path go up levels (using "../") rather than start at the root and go down. How can I get it to always upload to the /ROOT/IMAGES/ directory?

this should work
$newname = $_SERVER['DOCUMENT_ROOT']."/images/".$image_name;

Use:
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= '/images/';

Related

change wordpress image upload to another directory in public_html

I want to set my wordpress files to upload to /home/public_html/images instead of /wp_content/uploads, I have gotten to the point that it gives me the correct url for /home/public_html/images but I have to move the new files from /wp_content/uploads to /home/public_html/images for it to work
I have tried changing the upload path in wp-options.php but that just makes a subdirectory in my sites folder called public_html and puts the image there instead of actually putting it in /home/public_html/images
Try going to the wp_config.php file and adding this line of code:
define('UPLOADS', 'images');
This should put the images in a folder under /public_html instead of the typical /wp_content folder.
https://developer.wordpress.org/apis/wp-config-php/#moving-uploads-folder

Uploading a file with PHP and moving it

I have a form where users can upload images. I know how to do it with HTML/PHP and I know that I have to move the temp file to the permanent directory. Well this all works well when my file that processes the form, is in the root directory. But, I would like to have it work within another directory, on the same server.
The Structure is this:
ROOT
>images
>mobile
>upload.php
I would like to have the file put inside images, which is outside of the "mobile" directory.
My current upload path that does not work is:
define('UPLOADPATH', 'images/');
So essentially I need to go back one directory and then into images. How can I do this?
You need to add a .. To move to the parent directory. See this resource for more information

upload file outside of root folder

public_html/(root)index.php
/image/123.jpg
/subdomain(root)/upload.php
I have file upload.php can upload image, this file is locate in sub-domain folder (I have set sub-domain folder as another root)
I need to upload image to main domain, /image/ (image's folder)
however I could not find the right path. I have try ../ but it wont work above the root
anyone know how to solve this problem?
Use path like /home/content/html/images

Move uploaded file to root directory

I'm making a content management system for a website I built. I want the system to be discrete, so I made it exist in only one PHP file, called '_admin.php'. All the content displayed in this file comes from includes that I store in a sub-folder called 'admin' (out of the way).
The photos used on the website are stored in an 'assets' folder that also sits in the root dir. The admin page has direct access to the assets folder, as it is also in the root. But the upload file script sits a few directories into the 'admin' folder and I want the uploaded files to be stored in the assets folder.
The move_uploaded_file() method takes the destination path for the file, but it requires a direct path. I try using $_SERVER['DOCUMENT_ROOT'], but the resulting directory doesn't seem to have any of my files. If I use getcwd() in a doc in the root, it returns the actual file structure that I can use. The same if I echo out __FILE__. But I've experimented with this SERVER constant a lot and I can't locate my website with it.
Since the script that uploads the images is called as a form action, I can't pass the root directory as a variable.
Not really sure what I'm doing wrong, anyone have any ideas?
Thanks
edit **
//See if Files array contains new files
if (!empty($_FILES['file'])){
foreach($_FILES['file']['name'] as $key => $name){
$error = $_FILES['file']['error'][$key];
$temp_name = $_FILES['file']['tmp_name'][$key];
$dir = getcwd();
$move_file = move_uploaded_file($temp_name, "$dir/temp/$name");
if (($error == 0) && ($move_file)){
$uploaded[] = $name;
}else{
die($error);
}
}
echo __FILE__;
echo "<br/>";
echo __DIR__;
echo "<br/>";
echo $_SERVER['DOCUMENT_ROOT'];
exit();
}
The upload script I'm currently using. This script works fine because I'm storing the images in the same directory as the script. I just don't know how to store them in my root.
I would specify the full absolute file path if you can. I set this via define() in a config file for my CMS. On install you figure out what that path is and set it.
define("BASEFILEPATH", "/home/.../[webroot]"); // The base file path for the website
You may be looking for something more general, but you could have some sort of install script where the user can enter basic info into a form, such as username, pwd, etc. and you could have them enter this path as well.

How do I upload images to the correct path in PHP?

I have a website with the following path for images: domain.com/images
I am using a PHP script in the back-end to upload images to that folder BUT the page that lets me upload images to that path above is located at domain.com/administration/upload.php.
So now when I am using a script to write to domain.com/images this is what I am using:
$newname="/images/".$image_name;
For some reason it won't write to that folder and I get no errors and here's the link from where I got the script:
http://www.reconn.us/content/view/30/51/
What do I have to do to make it write to that domain.com/images path from domain.com/administration/upload.php?
You have to distinguish virtual server path from filesystem path.
There are no /images/ directory on your disk
but something like /home/www/user/public_html/images/
so, change your code to
$newname = $_SERVER['DOCUMENT_ROOT']."/images/".$image_name;
and it should be okay
You can try this for the exact path to folder
/* $_SERVER['DOCUMENT_ROOT'] will point to your root folder( www ) and after that your website folder name and them images folder name */
$newname = $_SERVER['DOCUMENT_ROOT']."/folder name/".$image_name;

Categories