Upload image from outside of webroot in webroot folder - php

I have folder structure like this
public_html
/images
outside
/file_uploader.php
that is, public_html is webroot folder.
outside is outside of webroot folder.
So, I want upload file in folder public_html/images.
I have in file outside/file_uploader.php this code:
move_uploaded_file(
$_FILES['img_name']['tmp_name'],
absolute/path/to/public_html/images/folder
);
But this returns error, that Unable to access to public_html/images folder.
Question: how can upload file from outside_of_webroot_file in webroot\folder ?

// see if you have permission to write to this folder
if(!is_writable(absolute/path/to/..)) {
chmod()
}
move_uploaded_file()

Related

How to create a shorcut in hostinger inside public_html folder? I'm using a laravel application

My folder of laravel application is outside my public_html folder because that is how it is set-up. My problem is everytime that I create a symlink or a shorcut for my images inside the public_html folder it says invalid link but when I tried to create a shorcut outside my public_html folder it works but I cannot access it publicly. Do you have anytips of how can I access the images or create a shorcut inside my public_html folder for my images?

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

Possible to upload from a subdomain to the main domain?

Ok so this is my server structure...
ROOT
-PUBLIC_HTML
--IMAGES
-SUBDOMAIN
As you can see, the subdomain is located on the root alongside the public_html.
How can i upload a file from the subdomain (subdomain.site.com) to the public_html/images (site.com/images) folder? or is this not possible?
Thanks.
When you upload your file just have your move_uploaded_file() move the file to the desired location in the main domain's directory.
If you're using open_basedir make sure you have rights to write to your main domain's directories.
Added the example code below - you will have to tweak it to make it work for you
The easiest way is to use an absolute path. You could use something like this
// let's assume your upload code is here: /var/www/subdomain.mysite.com/my-upload-code.php
// your desired location
$myImageDir = '/var/www/mysite.com/my-images/' ;
$fileName = basename($_FILES['pictures']['name']) ;
move_uploaded_file($_FILES['pictures']['tmp_name'], $myImageDir . $fileName) ;
In my case my sub domain and main domain were both inside the public_html.
public_html:
sub domain
uploads
uploading happens in sub domain. So in my file path variable I just added ../
ex: $path = "../uploads"
to go outside the current directory from subdomain to maindomain.

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

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