Why do I keep getting "directory does not exist?" - php

I cannot figure out what I am doing wrong here. The permissions for the directory I have for the file being created have write permissions all across the board. I keep getting "directory does not exist" Thanks for the help!
<?
//creates variables and calls the information from the server
$Name = $_POST['name'];
$desc = $_POST['desc'];
$website =$_POST['web'];
$email =$_POST['email'];
$cname =$_POST['cname'];
echo "your registered name is: ". $Name . ".<br/>";
echo "your registered description is: " . $desc . ".<br/>";
echo "your website address is: " . $website . ".<br/>";
echo "your Confirmation email has been sent to: " . $email . ".<br/>";
echo "your information has been stored, thank you! ";
$cname = trim($cname);
$filename = "data/clubinfo/$cname.txt";
$fp = fopen($filename,'a');
fwrite($fp,$Name);
fwrite($fp,"\n");
fwrite($fp,$email);
fwrite($fp,"\n");
fwrite($fp,$desc);
fwrite($fp,"\n");
fwrite($fp, $website);
fwrite($fp, "\n");
fwrite($fp,"__");
fwrite($fp, "\n");
fclose($fp);
?>

Most likely the script is assuming a different working directory to what you're presuming since you're using a relative path.
You'd be better off specifying the path absolutely or at least in relation to $_SERVER['DOCUMENT_ROOT'] even if you do:
$filename = $_SERVER['DOCUMENT_ROOT'] . "../data/clubinfo/$cname.txt";
The advantage of that is that it's outside your document root so it won't be served directly by your Web server. It will also work no matter the location of your script and will work no matter under what directory you install your Webapp, which can be an issue with dev vs prod deployments.

The data/clubinfo folder does not exist in the current directory.
You need to create it first. (By hand or in PHP)
Alternatively, the current directory might not be what you think it is.

Try using file_put_contents() like this:
file_put_contents("data/clubinfo/$cname.txt", implode("\n", $_POST));
If you want to this value by value you should also use the FILE_APPEND flag.

looks like you have not given the path off the root and the server is looking from the current location. try giving the path off the root.

Related

Store image in new directory in PHP

so i can upload my photo from my Android app fine to /var/www/html/ProductPhotos but when i want to get the name of the Product and use that as the name of the new directory and image name then its not working. I create the new directory and /var/www/html/ProductPhotos with 777 permissions (even though its super bad) but for now its what i need. here's my PHP code:
<?php
$ProductAccountName = $_POST['ProductAccountName'];
$ProductName = $_POST['ProductName'];
$ProductImage = $_POST['EncodedImage'];
$NewDirectory = "/var/www/html/ProductPhotos/" . $ProductAccountName;
mkdir($NewDirectory, 0777, true);
//$DecodedProductImage = base64_decode("$ProductImage");
//$ProductName = $ProductName .".JPG";
file_put_contents("/var/www/html/ProductPhotos/" . $ProductAccountName, $ProductName . ".JPG", $DecodedProductImage);
?>
You're using a comma instead of a period. And you're missing a slash.
file_put_contents("/var/www/html/ProductPhotos/" . $ProductAccountName . "/" . $ProductName . ".JPG", $DecodedProductImage);`
See the file_put_contents docs.
You may want to be put into place some checks to make sure the user doesn't use relative paths(using ../ as part the ProductAccountName, for example). Just be careful of the user using this to do malicious things.

Retrieve original uploaded filename using php on OpenShift

I would like to check that a file uploaded to my OpenShift app has a text extension (.txt or .tab). Following some advice given here I wrote the following code, with echoes added to help debug:
$AllowedExts = array('txt','tab');
echo "AllowedExts: " . $AllowedExts[0] . " and " . $AllowedExts[1] . "<br>";
$ThisPath = $_FILES['uploadedfile']['tmp_name'];
echo "ThisPath: " . $ThisPath . "<br>";
$ThisExt = pathinfo($ThisPath, PATHINFO_EXTENSION);
echo "ThisExt: " . $ThisExt . "<br>";
if(!in_array($ThisExt,$AllowedExts) ) {
$error = 'Uploaded file must end in .txt or .tab';
}
echo "error echo: " . $error . "<br>";
On uploading any file, the echoed response was:
AllowedExts: txt and tab
ThisPath: /var/lib/openshift/************/php/tmp/phpSmk2Ew
ThisExt:
error echo: Uploaded file must end in .txt or .tab
Does this mean that OpenShift is renaming the file upon upload? How do I get the original filename and then check its suffix? More generally, is there a better way to check the file type?
$_FILES['uploadedfile']['tmp_name'] contains the name of a temporary file on the server (which can be moved with move_uploaded_file()). If you want to check the original name of the uploaded file on the client machine use $_FILES['uploadedfile']['name'].
That's not an Open Shift issue, it's the standard way of PHP.
For further details see http://php.net/manual/en/features.file-upload.post-method.php
For other ways to detect the file type see http://php.net/manual/en/ref.fileinfo.php

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)

mkdir function, doesn't appear to be working

I have created a mkdir function in my php webpage but it doesn't appear to be working.
Here it is:
mkdir("Game/" . $user . "/" . $name . ".actibuild", 0777, true);
user and name are defined above. Here's the snippet of code:
if (isset($_POST['name'])){
if (isset($_POST['desc'])){
$name = mysql_real_escape_string($_POST['name']);
$desc = mysql_real_escape_string($_POST['desc']);
$user = $check_user['Username'];
mkdir("Game/" . $user . "/" . $name . ".actibuild", 0777, true);
mysql_query("INSERT INTO `games`(`creator`, `name`, `desc`) VALUES ('$user', '$name', '$desc')");
header('Location: Games.php');
}
}
It is correctly running those queries into the database, but it isn't creating those directories.
Can you help?
check the current directory with:
echo __FILE__;
or
echo getcwd();
and build your path relative to this reference.
or
you can use chdir("/") to set the root directory as the current directory, then try to create your path.
Do you know where PHP executes? You have a relative system path (starting with Game). Chances are, your directory is getting created (if permissions allow), but in a location relative to the working directory, which is not necessarily the same place where your PHP script lives.
You using relative paths, it may be not the place you think directories are created but current directory/web-root/etc.
Try:
$path = "Game/" . $user . "/" . $name . ".actibuild";
is_writable('.') || die(realpath('.') . ' is not writable');
mkdir($path, 0777, true) || die(realpath($path).' directory not created');
print_r(realpath($path));

PHP Upload Timeouts: More Efficient Upload Script?

I have written a pretty basic upload script that takes the file and uploads it using the standard move_uploaded_file method as you see below:
//UPLOAD IMAGE
$path = "../../clients/$realClient/" . $_FILES["image"]["name"][$x];
move_uploaded_file($_FILES["image"]["tmp_name"][$x], $path);
$displayPath = "private/clients/$realClient/" . $_FILES["image"]["name"][$x];
mysql_query("INSERT INTO images VALUES(NULL, '$displayPath', '$client', '$project', '$newWeight')") or die("Unable to Insert Image - Please Try Again");
echo "File Successfully Uploaded<br />";
This upload script works perfectly for most purposes. Here's my issue:
I have a standard shared hosting package so sometimes when the user tries to upload a file that takes over five minutes to upload (say a video or some other high res media), the server times out. The hosting company has said that as it's a shared hosting server they are unwilling to increase the timeout limit for me.
Is there a more efficient upload script that will allow files to go up in under five minutes or is there perhaps an alternative you could suggest?
Cheers,
Dan
The PHP script is run after the upload completes; so if there's a timeout during the upload, there's nothing you can do in the script (as it won't be run at all).
If the timeout occurs during the script, you could split it into multiple parts - have the first script just store the uploaded file and HTTP redirect to another, which will do the processing and database work. In the script you're showing, the processing seems simple enough, not sure if splitting that would help.
Assuming you're showing just a simplified version:
script1.php
// upload is complete
$fname= basename($_FILES["image"]["name"][$x]); //prevent directory traversal
$uniqid = uniqid("",true);
$path = "../../clients/$realClient/temporary/" . $uniqid . '/' . $fname;
// move to temporary location and redirect
move_uploaded_file($_FILES["image"]["tmp_name"][$x], $path);
header('Location: /script2.php?file=' . $uniqid . '/' . $fname);
script2.php
$path = $_GET['file'];
$uniqid = basename(dirname($path));
$fname = basename($path);
$temp_path = "../../clients/$realClient/temporary/" . $uniqid . '/' . $fname;
$final_path = "../../clients/$realClient/" . $fname;
move($temp_path,$final_path);
// do whatever processing is necessary
mysql_query("INSERT INTO images VALUES(NULL, '$displayPath', '$client', '$project', '$newWeight')") or die("Unable to Insert Image - Please Try Again");
echo "File Successfully Uploaded<br />";

Categories