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
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
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.
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
I am trying to copy a uploaded file which is in my directory called "upload" and is called image (5).jpg to my other directory called "images". This is my code.
<?php
copy(upload/image (5).jpg, images/);
?>
Am I doing this right?
First your image file name is awful, please do not use spaces in file names. Also do not use brackets in file name. Some systems like google app engine would easily mark that as invalid file name and ignore it.
Then adjust a little your line of code to look like this:
$old_file = 'upload/image_5.jpg';
$new_file = 'images/image_5.jpg';
$copied = copy(old_file, $new_file);
if ($copied) {
print "file " . $old_file . " is copied to " . $new_file;
} else {
print "error";
}
If you encounter an error maybe paths to files are not configured right. You do not have a write permission on that specific directory etc.
When I upload a file using this code, it puts a copy of the file in the "uploads" folder(which is what I want) but it also puts a copy in my root. I only want the files going to the uploads folder.
define ('GW_UPLOADPATH', 'uploads/');
$upfile= GW_UPLOADPATH . $_FILES['userfile']['name'];
if(is_uploaded_file($_FILES['userfile']['tmp_name']))
{
if(!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile)) //this is saying if the file isn't moved to $upfile.
{
echo 'Problem: could not move file to destination directory';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename: '; //this could be an attack b/c it might be from localhost.
echo $_FILES['userfile']['name'];
exit;
}
echo 'File uploaded successfully<br><br>';
What would be your temporary dir? Is it possible that somehow the uploaded file lands in the root but PHP can not delete it? Figuring this out requires a lot more knowledge about your setup.