Hopefully easy question, I have a desktop application that allows the user to upload a file to a server using a form, the form sends the data to a protected file on the site like this. Site_root/protected_folder/myfile.php . If you use php file upload commands normally you'd be operating in the 'protected_folder' directory, which I don't want.
I want to add stuff to the images file on the root directory like this Site_root/images/ , how would you go about doing this without going the ftp root?
The usual method is to call move_uploaded_file(), where you set the destination path to your liking. The file name in $_FILES['tmp_name'] normally points to a temporary folder and it's subject to be removed without prior notice.
You can either use an absolute path like /path/to/images/ or use a relative path like ../images/
Assuming you're using move_uploaded_file the second paramater takes the directory that you wish to upload to. Perhaps showing you code may help if this post doesn't.
move_uploaded_file() will allow you to place uploads relative to the root directory if you simply start your path with a slash like
$newFileDir = '/username/public_html/websiteroot/subdir/yourfile.jpg';
move_uploaded_file($_FILES['postname']['tmp_name'],$newfileDir);
you can simply use copy() and double dot (../) in path to specify root directory to copy the uploaded file. I'm using the same. You may want to change the file name so that there will be unique filename in the directory error will occur. extension will also be same.
//
$filename = stripslashes($_FILES['postname']['name']);
$extension = getExtension($filename);
$newfilename ='photo_your_filename'.time().'.'.$extension;
$newFileDir = '../subdir/'.$newfilename;
copy($_FILES['postname']['tmp_name'],$newfileDir);
Related
Im trying to rename an image that has a full stop in the name with php using the rename() function.
rename("images/cat.cute.jpg", "images/cute-cat.jpg");
Currently getting a "No such file or directory" error.
When you specify images/cat.cute.jpg, you are specifying a path relative to wherever your PHP script is actually executing. The error you are getting is simply telling you that there is no file called cat.cute.jpg in the folder images relative to the execution location of the script. Or, such a folder images does not exist. To fix this, you either need to specify the correct relative path, or you may specify an absolute path. Here is an example of using rename() with absolute paths:
rename("/images/cat.cute.jpg", "/images/cute-cat.jpg");
This assumes that there is an images/ folder relative to root, which may not be the case, but you can adjust as you need.
i have a constant variable named SADMIN
define('SADMIN','http://localhost/synthesis_study_material/student_admin/');
and i am trying to use SADMIN constant in my move_uploaded_file function
move_uploaded_file($_FILES['name']['tmp_name'], SADMIN."include/uploaded/epub/".$_POST['name']);
my current file is in
http://localhost/synthesis_study_material/synthesis_notes_admin/index.php
file is not getting uploaded in expected folder and move_uploaded_file is not giving any warning or error
The destination of move_uploaded_file needs to be a file path, not a url. To clarify this your webserver has a specified folder on the file system set as the web root. This might be something like '/var/www'. This means when you go to 'http://localhost/' it will look for a file such as '/var/www/index.php' or '/var/www/index.php'.
the destination might be an absolute path or relative, so you might supply 'synthesis_study_material/student_admin/' and that might be the same as '/var/www/synthesis_study_material/student_admin/' (depending on your web root and where move_uploaded_file is called from). Note you can use dirname(__FILE__) to get the path of the current php file. Also note that move_uploaded_file will not create directories for you if they don't exist.
I suggest starting with a simple example such as move_uploaded_file($_FILES['name']['tmp_name'], 'test-upload.txt'); to see if anything gets uploaded, and make modifications from there.
My Following code is working fine when I set
$target = "size_images/14_20131216231522_cashew.jpg";
But Not Working in Actual php Code when full web address path is given though 777 Unix access is granted to system.
I am trying upload an image from SubDomain to Main Domain, So i need to give Full Path.
Page: http://subdomain.examples.com/
Code:
$target = "http://examples.com/size_images/14_20131216231522_cashew.jpg";
move_uploaded_file($_FILES["item_image"]["tmp_name"],$target);
For your reference, following are values of above code line...
echo $_FILES["item_image"]["tmp_name"]; --> "/tmp/php6RNC28"
echo $target --> "http://examples.com/size_images/14_20131216231522_cashew.jpg"
Even tried with relative path instead of http, No luck : /home/direc/www/size_images
No use of placing error code. its not returning any error.
error_reporting(E_ERROR | E_PARSE);
Try this, You need to use only relative path instead of domain path. Domain path doesn't work
$target = "size_images/14_20131216231522_cashew.jpg";
move_uploaded_file($_FILES["item_image"]["tmp_name"],$target);
instead of ,
$target = "http://examples.com/size_images/14_20131216231522_cashew.jpg";
move_uploaded_file($_FILES["item_image"]["tmp_name"],$target);
Send a file via POST with cURL and PHP Ref: http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/
Try using:
$target = $_SERVER['DOCUMENT_ROOT']."/size_images/14_20131216231522_cashew.jpg";
If you want to upload from subdomain.mydomain.com to mydomain.com simply put the upload script on mydomain.com and then use a relative path.
You are misunderstanding how move-uploaded-file() works. It is similar to "File Save", namely you have to tell the PHP script a locally-writeable directory and file name where the file goes.
You mention you're trying to go from "subdomain" to "main domain"... if these two web urls are hosted on the same machine, this will be possible, you will just choose the directory that has the files for the "main domain" site.
It has to be a relative path.. absolute path would not work.
Also make sure that you know the difference between "/" and "./" But I would strongly recommend to either use configuration variables(in case of framework) OR constants to store file path.
Former point to root and later point to current directory..
I am trying to delete photo in php using unlink. I have used it earlier on other server but this time it is not working. I have used absolute path for a test but still does not works:
I have used it as:
unlink('img1.jpg');
and :
unlink('http://www.mysite.com/img1.jpg');
Please anyone having such experience?
url not allow in ulink function
can you please used this
It's better, also safety wise to use an absolute path. But you can get this path dynamically.
E.g. using:
getcwd();
Depending on where your PHP script is, your variable could look like this:
$deleteImage = getcwd() . 'img1.jpg';
unlink($deleteImage);
check this
bool unlink ( string $filename [, resource $context ] )
and
filename
Path to the file.
So it only takes a string as filename.
Make sure the file is reachable with the path from the location you execute the script. This is not a problem with absolute paths, but you might have one with relative paths.
Even though unlink() supports URLs (see here) now, http:// is not supported: http wrapper information
use a filesystem path to delete the file.
If you use unlink in a linux or unix you should also check the results of is_writable ( string $filename )
And if the function returns false, you should check the file permissions with fileperms ( string $filename ).
File permissions are usual problems on webspaces, e.g. if you upload an file per ftp with a ftp user, and the webserver is running as an different user.
If this is the problem, you have do to a
chmod o+rwd img1.jpg
or
chmod 777 img1.jpg
to grand write (and delete) permissions for other Users.
unlink($fileName); failed for me.
Then I tried using the realpath($fileName) function as unlink(realpath($fileName)); it worked.
Just posting it, in case if any one finds it useful.
php unlink
use filesystem path,
first define path like this:
define("WEB_ROOT",substr(dirname(__FILE__),0,strlen(dirname(__FILE__))-3));
and check file is exist or not,if exist then unlink the file.
$filename=WEB_ROOT."img1.jpg";
if(file_exists($filename))
{
$img=unlink(WEB_ROOT."img1.jpg");
}
unlink won't work with unlink('http://www.mysite.com/img1.jpg');
use instead
unlink($_SERVER['DOCUMENT_ROOT'].'img1.jpg');//takes the current directory
or,
unlink($_SERVER['DOCUMENT_ROOT'].'dir_name/img1.jpg');
There may be file permission issue.please check for this.
Give relative path from the folder where images are kept to the file where you are writing script.
If file structure is like:
-your php file
-images
-1.jpg
then
unlink(images/1.jpg);
Or there may be some folder permission issue. Your files are on a server or you are running it on localhost? If it is on a server then give 755 permission to the images folder.
you are using url insted of path, here is how you should do it...
i am assuming your are uploading the picture to public_html. i suggest you to create a folder for pictures.
unlink("public_html/img1.jpg");
I am currently working on a Joomla! website. I am using the Jumi extension (which allows custom scripts to be made and executed within Joomla! itself) to create a simple file upload tool. The problem is that I get the following error:
Warning: copy(C:/xampp/htdocs/images/1253889508.jpg) [function.copy]: failed to open stream: No such file or directory in C:\xampp\htdocs\Joomla\components\com_jumi\jumi.php(25) : eval()'d code on line 61
The offending code is as follows:
//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname='C:/xampp/htdocs/images/'.$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
The full code is available here. Once I get this working, I will be modifying it to meet the needs of my website.
I'm not sure if the problem is a permissions issue with Jumi or if there is some other problem. The best I can tell is that for whatever reason, the temp file is not being created.
Thanks for any tips you may have!
Try this:
if(move_uploaded_file($_FILES['image']['tmp_name'], $newname)){
// move worked, carry on
}
And use relative paths instead of absolute ones.
does your C:/xampp/htdocs/images directory actually exists?
if not create it manually or with mkdir()
also try to use the constant DIRECTORY_SEPARATOR instead of hardcoding slashes
Also you should use the move_uploaded_file() for this and not the copy() function.
And never hardcode absolute paths into your scripts! instead get the root path and preferibly set it as a constant, this is mostly done with the dirname() function in the entry file, but joomla allready has a constant you can use for this.
Forward slashes in the $namename path rather than backslashes?