PHP mkdir with form input and security - php

I'm trying to make the folder with mkdir but no success, I can see that the path is correct but $_post isn't getting the name of folder from form input ($_post['foldername' is empty) don't know what's the problem. (I have all the permissions to make the folder safe_mode is off

You need to use $_POST to get the filename.
As has been posted in the comments, you also need to do something with $_POST['filename'] to insure that the user is not trying to post a relative path to your script and trying to create folders in locations that you don't intend. At the very least make sure that the variable doesn't contain '..' Since you are prepending a path, I don't think that you have to worry about a direct path to '/' but you may also want to invalidate inputs with a '/' in them.

You could always try this:
<?php
include("models/db-settings.php");
include("models/config.php");
$foldername = $_POST["foldername"];
$filename = $foldername;
$path = __DIR__ . "/uploads/" . $loggedInUser->username;
$fullPath = $path . "/" . $filename;
if (!file_exists($fullPath)){
mkdir($fullPath, 0777);
echo "The directory was successfully created.";
}
echo $fullPath;
?>
<form action="mkdir.php" method="post">
<input type="text" name="foldername" id="foldername" value="FolderName">
<input type="submit" value="submit">
</form>

Change
if (!file_exists($path)) {
mkdir("$path/$filename", 0777);
echo "The directory was successfully created.";
}
to
if (!is_dir($path)) {
mkdir("$path/$filename", 0777);
echo "The directory was successfully created.";
}

Related

PHP creating a folder with the right path

<?php
if (isset($_POST['filename']) && isset($_POST['editorpassword']) && isset($_POST['roomname'])) {
$dir = $_POST['filename']; // This must match the "name" of your input
$path = "evo/" . $dir;
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
}
?>
I have this script where I'm trying to create a new folder. The script itself is ran inside of a folder called /evo and by using this code, it creates the folder in there. Where it needs to go is ../../creative however even if I try and use
$path = "./rooms/creative/" . $dir;
or something to that effect it creates it with the base folder as evo so it appears at:
../evo/rooms/creative (creating the folders that don't exist there with it as it should)
I'm just unsure what to write in for the path on where I need it created to find the right location.
Simplest solution is to remove the "evo" in $path = "evo/" . $dir;

Images are not shown and file name changes when uploaded to the database

I can't get the picture to display/show when viewing, although the files are already stored in the database (table 'menu') http://i.imgur.com/wo1w90H.png. Also when I upload the images all at once, their file name would change automatically. I don't know how and why this happens. I use array to upload multiple images.
if (isset($_POST["Submit"])) {
--some code here--
if (isset($_POST["id_list"])) {
// if id list available
foreach($_POST["id_list"] AS $id) {
--some code here--
/* Handle file upload */
if ($_FILES['upload']['error'][$id] == 'UPLOAD_ERR_OK') {
$path = "images/newmenu/";
$path_parts = pathinfo($_FILES["upload"]["name"][$id]);
$extension = $path_parts['extension'];
$picture = md5(uniqid()) . "." . $extension;
if (move_uploaded_file($_FILES['upload']['tmp_name'][$id], $path . "/" . $picture)) {
$update = " UPDATE menu
SET MenuPicture='$picture'
WHERE MenuID=$id";
$mysqli->query($update) or die(mysqli_error($mysqli));
}
}
}
}
}
}
Below is the form and yes it does include enctype="multipart/form-data"
<input type="file" multiple name="upload[' . $id . ']" value="' . $record["MenuPicture"] . '">
Filename changes because you are generating it this way
$picture = md5(uniqid()) . "." . $extension;
uniqid() is based on current time and hashing it will cause the filename to change everytime
When I upload the images all at once, their file name would change automatically
It was due to this:
$picture = md5(uniqid()) . "." . $extension;
// And later
move_uploaded_file($_FILES['upload']['tmp_name'][$id], $path . "/" . $picture)
Basically, you are moving your uploaded file to a new filename for your image file, which is generated using uniqid() and hashed with md5(), with the file extension appended at the end.
I can't get the picture to display/show when viewing
How are you trying to display the picture? Is it from web browser, or you go straight to the directory and open from there? What error(s) did you get, if any?
Actually, have you tried to go to the directory and see whether the file is created inside the images/newmenu/ directory?
Also, for the target upload directory, you might want to append it with $_SERVER['DOCUMENT_ROOT'] so that the target directory is not dependent on where your script is located, but it's always based on the root.
By the way, you might know already, but there is an entry in PHP manual page on uploading multiple files

move_uploaded_file failing with no error

This question has been asked similarly a few times, but those answer's didn't apply to the problem I'm have. I've checked them all.
Basically, the function move_uploaded_file is returning false every time, even though I feel like I have all my ducks in a row. There is no error, it just returns false.
I have checked the file that is being uploaded, it has no errors.
It may be a permissions problem, I tried to change the directory I'm uploading the images to using chmod(dir, 0777). If it were a permissions problem, I'm not sure if this would've fixed it. Edit - Checked iswritable(dir) of the directory and it says its writable.
I do have enctype="multipart/form-data" attribute set in my form.
This is my code:
function uniqueName()
{
$target = dirname(__FILE__) . '/TestProject/';
$uid = uniqid();
$ext = pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION);
$_FILES['photo']['name'] = $uid . "." . $ext;
if(move_uploaded_file($_FILES['photo']['name'], $target . $_FILES['photo']['name']))
echo("upload succeeded");
else {
echo("upload failed");
}
return $target . $_FILES['photo']['name'];
}
Am I missing anything? Any help would be appreciated.
Fixed it, the first parameter of move_uploaded_files() expects the tmp_name, not the name.
first off, and i sometimes forget make sure your form looks like this:
<form id="myform" name="myform" action="#" method="post" enctype="multipart/form-data">
key part of this is the
enctype="multipart/form-data">
and as far as uploading I use the following and it works everytime:
if ($_FILES["fuImg"]["error"] > 0)
{
echo "Error: " . $_FILES["fuImg"]["error"] . "<br>";
}
else
{
move_uploaded_file($_FILES["fuImg"]["tmp_name"], "../img/bands/" . $_FILES["fuImg"]["name"]);
}

symlink directory

Can I get an eyeball on my symlink?
I'm trying to download a file from one directory, while the file actually exists in another.
I've got the actual file, and the symlink in seperate subdirectories, but both reside in the public html(both are web accessible).
I've verified the file and file location on my (shared Linux) server by going to the file directly.
The link is being created (I've used readlink, is_link, and linkinfo), and I can see it when I FTP in.
I believe I am probably just having a misunderstanding of the directory structure.
I put the file here: ./testdownload/
I put the symlink here: ./testDelivery/
<?php
$fileName = "testfiledownload.zip";//Name of File
$fileRepository = "./testdownload/";//Where the actual file lives
$downloadDirectory = "./testDelivery/";//Where the symlink lives
unlink($downloadDirectory . $fileName); // Deletes any previously exsisting symlink (required)
symlink($fileRepository . $fileName, $downloadDirectory . $fileName);
$checkLink = ($downloadDirectory . $fileName);
if (is_link($checkLink))
{
echo ("<br>Symlink reads: " .readlink($checkLink) . "<br>");
echo ("<br>LinkeInfo reads: " . linkinfo($checkLink));
}
?>
<p><a href="<?php echo ("/testDelivery/" . $fileName); ?>"</a>SymLink</p>
<p><a href="<?php echo ("/testdownload/" . $fileName); ?>"</a>regular link</p>
Everything looks right to me....but the link won't work.
Help?
Ultimately, I will put the source data outside the public area...this is just for testing.
(I'm trying to find a better solution for download than chunking out fread which fails for poor connections. (200-400MB files))
My problem (appears) to be not providing the absolute path for the symlink.
I've added the absolute path below to the same code above, to give a working copy:
<?php
$absolutepath = ( $_SERVER['DOCUMENT_ROOT']);
$fileName = "testfiledownload.zip";//Name of File
$fileRepository = "/testdownload/";//Where the actual file lives
$downloadDirectory = "/testDelivery/";//Where the symlink lives
unlink($absolutepath .$downloadDirectory . $fileName); // Deletes any previously exsisting symlink (required)
symlink($absolutepath . $fileRepository . $fileName, $absolutepath. $downloadDirectory . $fileName);
$checkLink = ($absolutepath . $downloadDirectory . $fileName);
if (is_link($checkLink))
{
echo ("<br>Symlink reads: " .readlink($checkLink) . "<br>");
echo ("<br>LinkeInfo reads: " . linkinfo($checkLink));
}
?>
<p><a href="<?php echo ("/testDelivery/" . $fileName); ?>"</a>SymLink</p>
<p><a href="<?php echo ("/testdownload/" . $fileName); ?>"</a>regular link</p>
This original post, is a duplicate (though I didn't see it until now)
Create a valid symlink for PHP file
(Most of the answers given for that question were wrong however--but the original poster figured it out, and it worked for me too)

php: create directory on form submit?

I am wondering what I am doing wrong. I'm inside of PATH and I want to create a folder inside of PATH. I want to check if the folder already exists and, if not, create one. Getting the name of the folder from an input field with name of "dirname".
if (isset($_POST['createDir'])) {
//get value of inputfield
$dir = $_POST['dirname'];
//set the target path ??
$targetfilename = PATH . '/' . $dir;
if (!file_exists($dir)) {
mkdir($dir); //create the directory
chmod($targetfilename, 0777); //make it writable
}
}
It might be a good idea to make sure that the directory you are handling is indeed a directory. This code works... edit as you please.
define("PATH", "/home/born05/htdocs/swish_s/Swish");
$test = "set";
$_POST["dirname"] = "test";
if (isset($test)) {
//get value of inputfield
$dir = $_POST['dirname'];
//set the target path ??
$targetfilename = PATH . '/' . $dir;
if (!is_file($dir) && !is_dir($dir)) {
mkdir($dir); //create the directory
chmod($targetfilename, 0777); //make it writable
}
else
{
echo "{$dir} exists and is a valid dir";
}
Good luck!
Edited: comment was a good hint ;)
You have to use
!is_dir($dir)
instead of
!file_exists($dir)
it's not a file, it's a directory!
Good luck!
You can use is_dir().
#codeworxx file_exists can be used to check a directory as well..
http://www.php.net/manual/en/function.file-exists.php

Categories