PHP file upload in another directory - php

I am uploading files in php. The upload directory is
/var/www/html/oop/uploads/images/
But after uploading when i open the folder, it contains no image whatsoever. But when I try to include the image in a web page like this,
<img src="http://localhost/oop/uploads/images/1472722513.jpg" />
it is being included in the web page and works fine. I don't know how this is possible, I am using ubuntu 16.04.
my upload code
$file_path = '/var/www/html/oop/uploads/images/';
$name = $_SERVER['image']['name'];
$name = explode('.', $name);
$name = array_reverse($name);
$file_name = time() . '.' . $name[0];
$temporary_location = $_SERVER['image']['name']['tmp_name'];
if(move_uploaded_file($temporary_location, $file_path.$file_name)) {
echo "all ok";
} else {
echo $_SERVER['image']['name']['error'];
}

You didn't understand the concept of document root. A webserver does not reflect the local file system to public, mainly for security reasons.
When you are able to reference /var/www/../image.jpeg in your web browser, this actually points to a path relative to your document root e.g. /var/www/html/var/www/images/image.png
Read more about document root here
https://httpd.apache.org/docs/2.4/en/mod/core.html#documentroot

Related

failed to open stream: Permission denied open server

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

"No such file or directory" on localhost copy

EDIT: I'm pretty sure the issue has to do with the firewall, which I can't access. Marking Canis' answer as correct and I will figure something else out, possibly wget or just manually scraping the files and hoping no major updates are needed.
EDIT: Here's the latest version of the builder and here's the output. The build directory has the proper structure and most of the files, but only their name and extension - no data inside them.
I am coding a php script that searches the local directory for files, then scrapes my localhost (xampp) for the same files to copy into a build folder (the goal is to build php on the localhost and then put it on a server as html).
Unfortunately I am getting the error: Warning: copy(https:\\localhost\intranet\builder.php): failed to open stream: No such file or directory in C:\xampp\htdocs\intranet\builder.php on line 73.
That's one example - every file in the local directory is spitting the same error back. The source addresses are correct (I can get to the file on localhost from the address in the error log) and the local directory is properly constructed - just moving the files into it doesn't work. The full code is here, the most relevant section is:
// output build files
foreach($paths as $path)
{
echo "<br>";
$path = str_replace($localroot, "", $path);
$source = $hosted . $path;
$dest = $localbuild . $path;
if (is_dir_path($dest))
{
mkdir($dest, 0755, true);
echo "Make folder $source at $dest. <br>";
}
else
{
copy($source, $dest);
echo "Copy $source to $dest. <br>";
}
}
You are trying to use URLs to travers local filesystem directories. URLs are only for webserver to understand web requests.
You will have more luck if you change this:
copy(https:\\localhost\intranet\builder.php)
to this:
copy(C:\xampp\htdocs\intranet\builder.php)
EDIT
Based on your additional info in the comments I understand that you need to generate static HTML-files for hosting on a static only webserver. This is not an issue of copying files really. It's accessing the HMTL that the script generates when run through a webserver.
You can do this in a few different ways actually. I'm not sure exactly how the generator script works, but it seems like that script is trying to copy the supposed output from loads of PHP-files.
To get the generated content from a PHP-file you can either use the command line php command to execute the script like so c:\some\path>php some_php_file.php > my_html_file.html, or use the power of the webserver to do it for you:
<?php
$hosted = "https://localhost/intranet/"; <--- UPDATED
foreach($paths as $path)
{
echo "<br>";
$path = str_replace($localroot, "", $path);
$path = str_replace("\\","/",$path); <--- ADDED
$source = $hosted . $path;
$dest = $localbuild . $path;
if (is_dir_path($dest))
{
mkdir($dest, 0755, true);
echo "Make folder $source at $dest. <br>";
}
else
{
$content = file_get_contents(urlencode($source));
file_put_contents(str_replace(".php", ".html", $dest), $content);
echo "Copy $source to $dest. <br>";
}
}
In the code above I use file_get_contents() to read the html from the URL you are using https://..., which in this case, unlike with copy(), will call up the webserver, triggering the PHP engine to produce the output.
Then I write the pure HTML to a file in the $dest folder, replacing the .php with .htmlin the filename.
EDIT
Added and revised the code a bit above.

phpqrcode - save file to temporary directory

I'm trying to create a QR-code with the php library phpqrcode.
I want to save the file in a temporary file so I can use it afterwards. Something like this.
I'm using the Zend Framework and want to use the temporary directory. This is what I have so far:
require_once 'phpqrcode/qrlib.php';
require_once 'phpqrcode/qrconfig.php';
$tempDir = '/temp';
$fileName = 'test'.'.png';
$pngAbsoluteFilePath = $tempDir . $fileName;
$urlRelativeFilePath = '/temp' . $fileName;
// generating
if (!file_exists($pngAbsoluteFilePath)) {
QRcode::png('http://mylink.com/s/'.$quiz_url, $pngAbsoluteFilePath, 'L', 4, 2);
echo 'File generated!';
echo '<hr />';
} else {
echo 'File already generated! We can use this cached file to speed up site on common codes!';
echo '<hr />';
}
echo 'Server PNG File: '.$pngAbsoluteFilePath;
echo '<hr />';
// displaying
echo '<img src="'.$urlRelativeFilePath.'" />';
My output shows:
Server PNG File: /temptest.png
And an image that can't be found. Can somebody help me on the way?
EDIT:
When I tried to change the '/temp' to '/temp/' I get this warning:
Warning: imagepng(/temp/test.png): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/surveyanyplace/site/library/phpqrcode/qrimage.php on line 43
SECOND EDIT:
When I checked my hard drive I saw that he just saves images on my root map like 'temptest.png'... How can I make sure he save this on temp folder on my server?
The image can't be found by your browser because it's looking for it at the URL "http://domain.tld/temptest.png" which relates to location "/Applications/MAMP/htdocs/surveyanyplace/site/public/temptest.png" on your disk and is not where the file was saved (it's "/temptest.png" as you noticed).
In order to directly serve files via your web server (Apache) you have to make sure the image file is under it's DocumentRoot (typically the "public" folder of your Zend Framework application)
One way is to create the following directory : "/Applications/MAMP/htdocs/surveyanyplace/site/public/qrcodecaches" and change $pngAbsoluteFilePath and $urlRelativeFilePath as follows:
$pngAbsoluteFilePath = APPLICATION_PATH . '/../public/qrcodecaches/' . $fileName;
$urlRelativeFilePath = '/qrcodecaches/' . $fileName;
($tempDir can be removed)
NB: You might want to take a look to Zend_View_Helper_BaseUrl to make $urlRelativeFilePath more portable when dealing with applications inside a subdirectory

What to change the variable below to?

I have a destination variable where when I upload a file then it goes to this destination:
<?php
// Edit upload location here
$destination_path = getcwd().DIRECTORY_SEPARATOR;
$result = 0;
$target_path = $destination_path . basename( $_FILES['fileImage']['name']);
if(#move_uploaded_file($_FILES['fileImage']['tmp_name'], $target_path)) {
$result = 1;
}
sleep(1);
?>
What I want to know is how do I edit the upload destination? Lets say I want the files to upload to the destination to go to portal.hud.ac.uk/Upload_App/Files/ then how do I change this?
Just change $destination_path to whatever you want. The way it is now (getcwd().DIRECTORY_SEPARATOR), it will save to the current working folder (which unless changed is the folder containing the script). This is very unsafe because I could just upload a file called index.php and anyone visiting your site would run whatever malicious code I would choose to insert.
Much safer would be to save it outside the webroot, where it can't be accessed directly via HTTP. Example:
$destination_path = $_SERVER['DOCUMENT_ROOT']."/../uploads/";
This will save to a folder called uploads found in the folder that contains the web root (probably public_html).

php directory content list above root

how do i use php to access the directory above my site root, i need to specifically go up one directory and show contents to the user so they can pick from a couple different directories on the same level as public_html, navigate into them, and when clicking on a file serve it up? server is unix/apache
zipsanimspublic_htmlThank you ahead.
David
i found that if they know the file name it can be served to them by this... named image.php
then image.php?file=imagename.jpg
Thank you!
<?php
$file = $_GET['file'];
$fileDir = '/path/to/files/';
if (file_exists($fileDir . $file))
{
// Note: You should probably do some more checks
// on the filetype, size, etc.
$contents = file_get_contents($fileDir . $file);
// Note: You should probably implement some kind
// of check on filetype
header('Content-type: image/jpeg');
echo $contents;
}
You go up one directory using the .. link. Example:
<?php include("../foo.bar"); ?>
Note that if you're on shared hosting, there's a good chance that the server won't let you do this.

Categories