PHP - Upload file error - Permission denied - php

i am trying to run this simple php code:
<?php
$dir = getcwd()."\uploads\ ";
if($_FILES['myfile']['error'] != 0)
{
echo "Error uploading the file: {$_FILES['myfile']['error']}";
}
if(move_uploaded_file($_FILES['myfile']['tmp_name'], $dir . $_FILES['myfile']['name'])) {
echo 'Success!!!';
} else {
echo 'Error.';
}
?>
I am getting this problem:
failed to open stream: Permission denied in... on line 7 Unable to
move ... to ... on line 7
Line 7 is: move_uploaded_file
I understand that maybe the problem is with the folder permission, and i need to use CHMODE command.
But i don't understand how to use it and where do i put it in my code.

Use slashes /instead of backslashes. And check permissions to target dir.
$dir = getcwd() . "/uploads/";

Well, you can call chmod() before the line with move_uploaded_file() for example like this
chmod($dir, 755);
You can find more information about chmod() on the documentation sites

Related

In php code, use shell_exec correctly in my case

I have question about how to save log in correctly directory. My php code:
public static function log(test $testination) {
echo($testination->getDetails()."\n");
$log_filename = shell_exec("cd; ~/log/");
$log_filename2 = "log";
if (!file_exists($log_filename2))
{
mkdir($log_filename, 0777, true);
}
$log_file_data =$log_filename.'/logs_' . date('d-M-Y') . '.log';
file_put_contents($log_file_data, var_dump($testination) . "\n", FILE_APPEND);
}
in this code, I create a folder in the same place as my PHP file, then create folder log if this not exist.
I want to save in begining (just cd) then create log folder if this not exist I try this code
$log_filename = shell_exec("cd; ~/log/");
i get warning/error
PHP Warning: file_put_contents(/logs_18-Aug-2020.log): failed to open stream: Permission denied in /test/test/Helper.php on line 318
Any idea how to do it? Please im stuck.
Any help will be appreciated.
That's not how you should use file_put_contents: it does not know anything about that attempt to change the directory, as this will not affect the execution of PHP. If you want to get the directory of the current PHP file, you can use basename(__DIR__)
Additionall, cd; ~/log/ won't work in your shell as well.

PHP copy() failed to open stream: Invalid argument

I am trying to upload a file from my brower->PHP script->Server.
My browser and PHP script lie on the same server. I am using xampp on this. The server where I have to upload this file is different.
Here is my code:
$fdir = "http:\\\\myip\\D:\\errors\\";
$ffile = $fdir.basename($_FILES['myfile']['name']);
if (copy($ffile,$_FILES['myfile']['tmp_name'])) {
echo "<br />"."File uploaded successfully";
} else {
echo "<br />"."Error in uploading file";
}
I am getting the following error:
copy(http:\myip\D:\errors\IMG-20150424-WA0004.jpg): failed to open stream: Invalid argument in C:\xampp\htdocs\BS\myphp1.php on line 54
Any help would be appreciated.
EDIT:
I edited my code to remove http:// and use the name of the server.
Also I am now using move_uploaded_file instead of copy
$fdir="\\\\TESTSRV\\D:\\errors\\";
$ffile=$fdir.basename($_FILES['myfile']['name']);
move_Uploaded_file($_FILES['myfile']['tmp_name'],$ffile);
But Still it gives me the error
move_uploaded_file(\TESTSRV\D:\errors\IMG-20150424-WA0004.jpg): failed to open stream: Invalid argument in C:\xampp\htdocs\BS\myphp1.php on line 54
This is not the way to copy uploaded files - you need to use the function move_uploaded_file:
if (move_uploaded_file($_FILES['myfile']['tmp_name'], "c:\\path\\to\\file")) {
echo "<br />"."File uploaded successfully";
} else {
echo "<br />"."Error in uploading file";
}
Also I don't think your $fdir variable is a valid path in Windows. Basically the second argument of move_uploaded_file should be the target path, where you'd like to move the file.

fopen error for creating a new file?

I was following a guide for creating a new file in php and I was using the following code:
function create($user, $name) {
/* Later on, this will connect to another server*/
$dir = $user->getFolder() . "/Projects/". $name;
if(file_exists($dir)) {
$this->error = "Directory: " . $dir . " already exists.";
} else {
mkdir($dir);
//Create the users.json file and add the owner
$json = fopen($dir . "/Data/users.txt", "w") or die("Cannot open file");
fclose($json);
}
}
The directory gets created but I receive the following error: "Warning: fopen(Jake/UserFolder//Projects/test/Data/users.txt): failed to open stream: No such file or directory in D:\xampp\htdocs\Collabs\Objects\Scripts\Project.php on line 14
Cannot open file"
The path shown in your error looks like it could be the problem.
Jake/UserFolder//Projects/test/Data/users.txt
There are two slashes between UserFolder and Projects. It looks like changing your code to
$dir = $user->getFolder() . "Projects/". $name;
Would get rid of the extra slash.
A couple of things that might have gone wrong:
You didn't pass path with a leading /, so fopen() will search relatively to the directory the script is executing in. I assume you'll want to pass an absolute path
you are testing for file_exists(), you should also check for is_dir(), to catch and handle the cases where a file with the same path already exists
you might consider calling mkdir($dir, 077, true) in order to create
the intermediary directories (see the mkdir() documentation)

Deleting files in higher directory

I'm having problems deleting a file from a higher directory, I found this post and tried it but no luck....:
gotdalife at gmail dot com 25-Sep-2008
02:04
To anyone who's had a problem with the
permissions denied error, it's
sometimes caused when you try to
delete a file that's in a folder
higher in the hierarchy to your
working directory (i.e. when trying to
delete a path that starts with "../").
So to work around this problem, you
can use chdir() to change the working
directory to the folder where the file
you want to unlink is located.
<?php
> $old = getcwd(); // Save the current directory
> chdir($path_to_file);
> unlink($filename);
> chdir($old); // Restore the old working directory ?>
here is the code that I currently have:
session_start();
if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] !=md5($_SERVER['HTTP_USER_AGENT']))){
require_once ('includes/login_functions.inc.php');
$url = absolute_url();
header("Location: $url");
exit();
}
$folder = $_GET['folder'];
$filename = $_GET['name'];
$path = "../gallery/photos/$folder";
if (isset($_POST['submitted'])) {
if ($_POST['sure'] == 'Yes') {
$old = getcwd(); // Save the current directory
chdir($path);
unlink($filename);
chdir($old); // Restore the old working directory
}
else{
echo '<p>The photo has NOT been deleted.</p>';
}
}
I'm getting the error message :
Warning: unlink() [function.unlink]:
No error in
J:\xampp\htdocs\bunker\admin\delete_file.php
on line 37
line 37 being:
unlink($filename);
can anybody see what I've done wrong?
I always use absolute filepath names.
I'd define the filedir as a constant in your config, then concatenate so you have an absolute filepath, then make a call to unlink().
Btw: I hope you know your code is highly insecure.
See here:
http://bugs.php.net/bug.php?id=43511
and here
http://php.bigresource.com/Track-php-03TimDKO/
http://www.phpbuilder.com/board/showthread.php?t=10357994
Though I wouldnt recommend doing this, as per the comments above. Is there the option for a different approach?

mkdir Function Error

I am stuck on a simple script. I have tried Googling it and I have tried all of the stuff I found on Google but none of it has worked for me. I am trying to you the mkdir function but I keep getting an error that says: Warning: mkdir() [function.mkdir]: No such file or directory in /home/content/l/i/n/web/html/php/mkdir.php on line 6
Here is my code:
<?php
//Create the directory for the user
$directory = time() . rand(0, 49716) ;
if (mkdir("/users/$directory", 0777)) {
echo '<p>Directory was created successfully.</p>' ;
} else {
echo '<p>Directory was NOT created.</p>' ;
}
?>
I am placing the script called mkdir.php in the same folder as the users folder. The users folder has the permissions set to 0777. I am not sure why I keep getting this error because the users folder does exists. Any help is greatly appreciated.
if (mkdir("users/$directory", 0777)) {
:-)
Nevermind I figured it out. It was just that the first slash in "/users/$directory" should be "users/$directory".

Categories