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
Related
My aim is to download multiple files into the folder on my localhost. I am uploading them using the HTML form.
Here is the code (really sorry that I can't give a link to the executable version of the code because it relies on too many other files and database if anyone knows the way then please let me know)
foreach ($_FILES as $value) {
$dir = '/';
$filename = $dir.basename($value['name']);
if (move_uploaded_file($value['tmp_name'],$filename)) {
echo "File was uploaded";
echo '<br>';
}
else {
echo "Upload failed";
echo '<br>';
}
}
So this little piece of code give me an error:
And here are the lines of code:
The problem is that the adress is correct, I tried enterring it into my file directory and it worked fine, I have seen some adviced on other people's related questions that // or \ should be used instead, but my version works just fine! Also I have checked what's inside the $_FILES and here it is if that's required for someone trying to help:
Thank you very much if anyone could help!!
You are trying to move the file to an invalid (or non-existent) path.
For the test you will write
$dir = 'c:/existing_dir/';
$filename = $dir.basename($value['name']);
If you want to move the file to a folder that is relative to the running file try
$dir = '../../directory/';// '../' -> one directory back
$filename = $dir.basename($value['name']);
By starting your file path with $dir = '/'; you are saying store the file on the root folder, I assume of C:
Apache if correctly configures should not allow you access to C:\
So either do
$dir = '../';
$filename = $dir.basename($value['name']);
to make it a relative path or leave the $dir = '/'; out completely
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 trying to upload a client file to my server (from an html form using a "post" method), run a program on the $upldfile variable and then display the program results as downloadable links for the client.
My code is listed below and every time I run this I get the "file upload failed" notice.
Does anyone know if this is to with a permissions based problem or a server error or a code issue?
Thank you all in advance for any help offered
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$destination_path = getcwd().DIRECTORY_SEPARATOR;
$target_path = $destination_path . "uploads/" . basename( $_FILES["file"{["name"]);
$upldfile = move_uploaded_file($_FILES['file']['tmp_name'], $target_path);
if ($upldfile){
echo "<p>File upload success.</p>";
} else {
echo "<p>File upload failed.";
}
ANSWER:
Changing the permissions appropriately,
and also
modifying the php.ini file to allow larger file uploads.
The code itself in the original file was correct.
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!
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.";
}