I'm using HTTP post to upload image to server and I have the following PHP code:
$base_path = "";
$target_path = $base_path . basename ( $_FILES ['uploadfile'] ['name'] );
if (move_uploaded_file ( $_FILES ['uploadfile'] ['tmp_name'], $target_path )) {
echo "Good";
} else {
echo "FAIL";
}
I'm sure the image has been uploaded to temp. But no matter what, I just can't store image file. my current permission is 664 for testing.
You need to set your $base_path variable to an absolute path to where you are storing the file. ( ie. /path/to/your/document/root/image/directory/ )
Additionally, make sure the directory you will be storing the images in is either owned by the apache user or that it is writable by the apache user (chmod 777).
Try this:
Pl check the uploaded path is correct before move the file
&&
Set the folder permission to 777 where you upload the file.
Thanks!
Related
I am using move_uploaded_file function to save my file into two folders, the folders name are uploads_meeting_document and uploads_filing_file. It just can let me upload my file to this folder name uploads_meeting_document, it can't save to uploads_filing_filefolder. Anyone can guide me which part I have problem in below the coding:
<?php
require_once("../conf/db_conn.php");
// Getting uploaded file
$file = $_FILES["file"];
// Uploading in "uplaods" folder
$pname = date("ymdhi")."-".$_FILES["file"]["name"];
//$title_name = $_FILES["file"]["name"];
$tname = $_FILES["file"]["tmp_name"];
$uploads_dir = 'uploads_meeting_document';
move_uploaded_file($tname, $uploads_dir.'/'.$pname);
$uploads_dir2 = 'uploads_filing_file';
move_uploaded_file($tname, $uploads_dir2.'/'.$pname);
?>
Below is my file path need to save to these folders(red arrow there)
In your example, the second move_uploaded_file does not work, because the file was already moved to /upload_meeting_document
You will need to copy your file from there:
...
$uploads_dir2 = 'uploads_filing_file';
copy($uploads_dir.'/'.$pname, $uploads_dir2.'/'.$pname);
In case this does not work, you may have insufficient permissions for the /uploads_filing_file directory. Chech its owner and permissions.
I am having a problem with move_uploaded_file().
I am trying to upload a image path to a database, which is working perfectly and everything is being uploaded and stored into the database correctly.
However, for some reason the move_uploaded_file is not working at all, it does not produce the file in the directory where I want it to, in fact it doesn't produce any file at all.
The file uploaded in the form has a name of leftfileToUpload and this is the current code I am using.
$filetemp = $_FILES['leftfileToUpload']['tmp_name'];
$filename = $_FILES['leftfileToUpload']['name'];
$filetype = $_FILES['leftfileToUpload']['type'];
$filepath = "business-ads/".$filename;
This is the code for moving the uploaded file.
move_uploaded_file($filetemp, $filepath);
Thanks in advance
Try this
$target_dir = "business-ads/";
$filepath = $target_dir . basename($_FILES["leftfileToUpload"]["name"]);
move_uploaded_file($_FILES["leftfileToUpload"]["tmp_name"], $filepath)
Reference - click here
Try using the real path to the directory you wish to upload to.
For instance "/var/www/html/website/business-ads/".$filename
Also make sure the web server has write access to the folder.
You need to check following details :
1) Check your directory "business-ads" exist or not.
2) Check your directory "business-ads" has permission to write files.
You need to give permission to write in that folder.
make sure that your given path is correct in respect to your current file path.
you may use.
if (is_dir("business-ads"))
{
move_uploaded_file($filetemp, $filepath);
} else {
die('directory not found.');
}
Greetings anyone who is reading this!
Currently I have a php page that allows the user to upload a file to my server at a specified directory. Here is what I use to do that:
if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1) {
chmod($uploadpath, 0777);
$uploadpath = $uploadpath . basename( $_FILES['fileup']['name']); // gets the file name
$sepext = explode('.', strtolower($_FILES['fileup']['name']));
$type = end($sepext); // gets extension
list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']); // gets image width and height
$err = ''; // to store the errors
// If no errors, upload the image, else, output the errors
if($err == '') {
if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) {
chmod($uploadpath, 0777);
echo '<br/><br/>File successfully uploaded:' .$uploadpath.'</b>';
}
else echo '<b>Unable to upload the file.</b>';
}
else echo $err;
}
The uploading part works like a charm and the file is uploaded fine to the server however once uploaded, I am unable to delete the file as another user. Currently the user uploading the file would be the Apache user "www-data".
I do believe that the issue has to do with permissions being set for the uploaded file which I have tried using:
chmod($uploadpath,0777);
This does not help though.. The file still can't be removed. I can remove it through SSH if I am root using "rm -rf /directorypath" but can't delete it if I browse to it.
Any suggestions? Any help is greatly appreciated!
if you want to remove a file you can use php unlink ! And if thats a directory then rmdir !
you could use unlink('upload_folder/'.$image['file_name']);
Try to allow permission to your image folder too not just the files underneath.
chmod($upload_path, 0777);
before the line
$uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);
Note that 777 is not super safe anyway. Best practice is to manage users with the group www-data
In this PHP code I'm going to upload a file (sent from AS3) to a directory that already created for each user with same name his username. The problem is that I don't know how to move the file into a folder associated to a user. If a user doesn't have his own folder, some code should be able to get the user's name from $_SESSION['myusername'] and then dynamically create it and then move the file:
<?php
session_start();
$username =$_SESSION['myusername'];
$uploads_dir = $_SERVER['DOCUMENT_ROOT'].'/upload/'.'/$username/';
if ( ! is_dir($uploads_dir)) {
mkdir($uploads_dir);
}
if( $_FILES['Filedata']['error'] == 0 ){
if( move_uploaded_file( $_FILES['Filedata']['tmp_name'],
$uploads_dir.$_FILES['Filedata']['name'] ) ){
exit();
}
}
echo 'error';
exit();
?>
But this code move file into "upload" directory and if the uploaded file name be xxx then file name change to xxx$username. How can do this please?
You have the right idea, you just need to also add the file name to the end of your path, something like:
$uploads_dir = "upload/".$username."/".$_FILES['Filedata']['name']
Then use move_uploaded_file() like so:
move_uploaded_file( $_FILES['Filedata']['tmp_name'],
$uploads_dir )
Also, its always a good idea to go ahead and make sure the directory exists before hand with file_exists().
I've also found that move_uploaded_file() likes full paths for the destinations, you can use $_SERVER[DOCUMENT_ROOT] to get this
Whenever I try to move a file it does not work and shows "Image file not uploaded"... I just want to know where the error is...
$target = '/var/www/student/public/myimage.jpg';
$destination = '/var/www/student/public/images/myimage.jpg';
if( move_uploaded_file( $target, $destination ) ) {
echo "Image file is successfully loaded";
} else {
echo "Image file not uploaded.";
}
I have checked error log (tail -f /var/log/apache2/error.log) but found nothing.
target and destination both directories have 777 permissions.
Can someone tell me that how to find out the error. Any idea ?
If you are not using HTTP POST upload method then you can use rename()
rename($target, $destination);
Has the file been uploaded in the current request?
move_uploaded_file will refuse to move files that are not uploads. (i.e. $target must equal $_FILES[$field_name]['tmp_name']
If it has been uploaded previously, move_uploaded_file will refuse to work (if it is even still there - PHP will delete it if you don't handle the file on that upload if I remember correctly)
If it is in fact not a file that has been uploaded with this request you'll want to use rename
move_uploaded_file() only works on http post files. http://php.net/manual/en/function.move-uploaded-file.php
to move a file already on the server, you will have to copy the file and unlink the old file
$target = '/var/www/student/public/myimage.jpg';
$destination = '/var/www/student/public/images/myimage.jpg';
if (copy($target, $destination)) {
unlink($target);
} else {
echo "Unable to copy $target to $destination.";
}