I am new to php.
I am using below code to upload image in local folder path "uploads/profilepics/".
move_uploaded_file
(
$_FILES["file"]["tmp_name"],
"uploads/profilepics/".$filenameWithUsername
);
It is working fine.
In remote server, my folder structure look like this, /public_html/myfamily/uploads/profilepics
I would like to know a good approach to store the image in remote server, if the code need some improvement?
I would use the __DIR__ magic constant (if youre lower than php 5.3 dirname(__FILE__)) to make sure your path is the same on local as on your server.
This will give you a fullpath to your dir no matter on what machine you are running.
So in your case id say try:
move_uploaded_file(
$_FILES["file"]["tmp_name"],
__DIR__ . "/uploads/profilepics/".$filenameWithUsername
);
if it doesn't work
echo __DIR__ . "/uploads/profilepics/".$filenameWithUsername;
See what it shows and edit the path to where it needs to go if it isn't correct
Related
I am working on a project with a lot of includes and requires. I don't want to change this everytime I deploy from localhost to my live server.
So I thought to use $_SERVER['DOCUMENT_ROOT'], which worked fine until I checked my git which pushes directly to a backup folder on my live server. It is not in the root, but one level lower.
All the files in this backup folder that use include or require get their files from my server root (public_html/) not the current folder the project is in (public_html/backup/).
So this wouldn't work. After that I tried getcwd();, this fixed above situation but now I can't include files on my localhost.
So now I thought to make a check to see if the file is currently on my local pc or on my live server like this:
$checkpath = getcwd();
$localcheck = 'C:\\';
// Test if path contains C:\
if(strpos($checkpath, $localcheck) !== false){
$absolute_path = $_SERVER['DOCUMENT_ROOT'];
} else{
$absolute_path = getcwd();
}
My only problem is how can I include this for all files when including is my issue?
Maybe there is a better way to get a path that always works no matter if it is live or local?
You can use set_include_path($absolute_path); to change the directory include/require looks from. You would then technically only need to call this once in every PHP request. Beware that you need to provide the actual path to set_include_path. For example, you would need to provide "/home/username/directory" instead of "~/directory".
set_include_path documentation.
on my project folder there is images/committee/images.jpg . This where the uploaded images are moving to
if(move_uploaded_file($image_temp,"images/committee/".$new_image_name))
Now what m trying to achieve is that.. On the Project folder there is a folder names as
user/images/committee/target.jpg this where i want to copy the image to.
copy("images/committee/".$new_image_name, "user/images/committee/".$new_image_name);
i tried using this.. but i think m giving wrong path. can some one help
if you use absolute paths you should be OK. ie: copy( __DIR__ . "/images/committee/".$new_image_name, __DIR__ . "/user/images/committee/".$new_image_name );
This worked for me. Do remember to check if you are in right directory if it is not working.
In my website I have the following 2 functions. The first one takes the FTP connection and creates a directory in the specified path. server.com/Host_path/DirectoryName for example. This piece of code works perfectly fine. I have an upload function that uploads file test.txt to the folder DirectoryName and the second function is supposed to download that file to my local Windows machine. However i get the error message Unable to download the specified file. Please check your path. With no indication as to which path is wrong.
$path = 'server.com/' . $result['path'];
$this->ftp->mkdir($path);
When I echo the remote and local paths I get remote = server.com/Host_path/DirectoryName/test.txt and local = C:\Users\Owner\Desktop\test.txt . Based on the fact that the above function works but the below function does not, I believe that the Windows path is wrong but it could be a difference in functions.
$remotePath = 'server.com/' . $result['path'];
$localPath = 'C:\\Users\\Owner\\Desktop\\test.txt';
$this->ftp->download($localPath, $remotePath, 'auto');`
Please Help!
For remotePath, use absolute path starting with "/"
I'm trying to upload images to server, create a thumbnail from the uploaded image, and save the relative paths to mysql database . . . . The image path is
/mysite/images/
from my site root (which is wamp/www/mysite/images in my local testing environment)
Now, I'm performing a directory check with
if(!is_dir($path) && !is_writable($path)){
throw new Exception ("$path is not valid .. ");
}
where path is /mysite/images/ , thinking that initial '/' will denote the root.
But, the script was throwing an exception saying invalid path name, and my
move_uploaded_file($tmp_name, $path.$filename)
too was not working.
PS: the path mysite/images/ also throws an error and the file is not moved
Now, I have worked around by appending document root manually to path name for both directory checks and move file , which is now looking like
if(!is_dir($_SERVER['DOCUMENT_ROOT'].$path) && !is_writable($_SERVER['DOCUMENT_ROOT'].$path)){
throw new Exception ("$path is not valid .. ");
}
move_uploaded_file($tmp_name, $_SERVER['DOCUMENT_ROOT'].$path.$filename)
I also changed the path to
mysite/images/
removing the initial slash intended for site root. This is working perfectly.
But, I cannot understand why directory check and move uploaded file snippets are not taking site root relative links.
Any ideas is appreciated.
The problem is with the path representation.
In linux /mysite will try to access the directory in the root level of file system. Windows this might not create an error. for is_file() kind of API's it should the absolute path and not relative ones.
Your website path is not wamp/www/mysite/images.
You are using a web server, you cant access the drive like: c:/wamp ....
Once online, how will you access your files? Linux does not have c:/ ...
Your website path is: http://localhost/mysite/
Your image path is: http://localhost/mysite/images or ./images if your code is correctly written.
so, do this:
define('ROOT', $_SERVER['DOCUMENT_ROOT']);
include_once(ROOT."/mycfgfile.php");
or
include_once(ROOT."/includes/mycfgfile.php");
< Referring to Make path accessible at various level of folder on PHP >
The code below:
require_once __DIR__ . '/../FolderB2/PageB21.php';
This works locally, but not when I have uploaded the page to the server. Is my hosting company restricting access to this?
If you run this code, do you get the exact path you are expecting?
echo dirname(__FILE__) . '/../FolderB2/PageB21.php';
Perhaps the relative path is wrong or maybe there is a mistake in the casing (i.e. if your local machine is windows, paths are not case sensitive, but they would be on a Linux server).
Perhaps you can use $_SERVER['DOCUMENT_ROOT'] . '/path/to/file.php' if your host runs PHP less than 5.3