php unlink file renovated - php

I want to delete with php (unlink function) file which is out of webroot. my web root is in
C:\server\webroot\project\... in webroot I have folder named project and in there I have .php files.
whats about files directory. it is situated C:\server\mp3_files...
Also I've created in httpd.conf Alias("mp3") of mp3_files directory
I am writing this script in C:\server\webroot\project\test.php
script is like so =>
function delete($filename){
if (unlink("/mp3/" . $filename)){
echo "Deleted";
} else {
echo "No";
}
}
delete("file.txt");
this script gives me in php-errors => PHP-WARNING No such file or directory
also I have in (test.php) html form this =>
Download
And this works (It opens this file.txt)
So I'm wondered why can't delete with marked function "delete($filename)" ?

"/mp3/" . $filename is an absolute filepath, not relative to the webserver root, so it's assuming that you have an mp3 directory under your filesystem root when you should be looking under /server/mp3
EDIT
And is it /server/mp3 or /server/mp3_files
your post seems to contradict your code

File function in PHP go from the file system root.
You should write:
function delete($filename){
if (unlink("C:\\server\\mp3_files\\" . $filename)){
echo "Deleted";
} else {
echo "No";
}
}
delete("file.txt");

To make sure the internal PHP file path cache gets the correct information, reset with it with clearstatcache() before and after the unlink. Normally the path cache is reseted after every PHP function which is related to file manipulation. Reseting the cache is required if you remove files with shell_exec('rm file.txt') or similar.
See http://php.net/manual/ini.core.php#ini.realpath-cache-size and http://php.net/manual/ini.core.php#ini.realpath-cache-ttl

Related

"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.

DS_Store file is created on ZipArchive::extractTo

I'm creating a PHP script, which supposed to extract a zip archive stored on the php file directory to a folder.
Everything works well, but when I check te result, I find 2 folders under the directory: a folder with the name of the zip archive, and another folder named __MACOSX. I don't know how this folder came there, especially as I'm using Windows 7. Second, in each folder there is a file called .DS_Store.
Now, I don't know how these things got there. This is my code:
$zip = new ZipArchive;
if ($zip->open('File.zip')) {
$path = getcwd() . "/details/" . trim($id) . "/";
$path = str_replace("\\","/",$path);
echo $path;
echo $zip->extractTo($path);
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
This is the only code that extracts the zip file, or touching it, and as you can see, there is nothing like __MACOSX or .DS_Store.
Can you please help me?
File.zip originated on a OSX system. __MACOSX and .DS_Store have 0 usage or bearing on any other OS. Delete / Ignore them and keep trucking.
As an aside, you may want to add the stated file system objects to your project .gitignore.
https://superuser.com/questions/104500/what-is-macosx-folder
https://en.wikipedia.org/wiki/.DS_Store

PHP - Creating .txt document in Ubuntu

I am having trouble using fopen() to create a text document for later use as a cookie file.
I have read the documentation for this function, but to no avail.
Notes:
Ubuntu
read / writable ("w+")
I have tried several storage locations including:
/home/jack/Desktop/cookie
/var/www/cookie
/home/jack/Documents/cookie
PHP
echo "debug";
echo "\r\n";
$cookie = fopen("/home/jack/Documents/cookie", "w+");
fclose($cookie);
if(!file_exists($cookie) || !is_writable($cookie))
{
if(!file_exists($cookie))
{
echo 'Cookie file does not exist.';
}
if(!is_writable($cookie))
{
echo 'Cookie file is not writable.';
}
exit;
}
Result
file is not created
Output to browser: debug Cookie file does not exist.Cookie file is not writable.
Other Fun Facts
I have tried using fopen(realpath("/home/jack/Documents/cookie"), "w+")
echo "\r\n" gives a space. Why not a newline?
I believe the problem must be something to do with my permissions to create the file, but I have no problem "right-click" creating the text document on the Desktop.
THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS
echo "debug";
echo "\n";
$jack = "jack";
$cookie = "/home/jack/Documents/cookie";
touch($cookie);
chmod($cookie, 0760);
if(!file_exists($cookie) || !is_writable($cookie))
{
if(!file_exists($cookie))
{
echo 'Cookie file does not exist.';
}
if(!is_writable($cookie))
{
echo 'Cookie file is not writable.';
}
exit;
}
fclose($cookie);
THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS
Instead of fopen()..
touch() to create
chmod() for permissions
I also added user name jack to www-data group.
chmod($path, 0760) group read / write
Reference
chmod() octal values here.
Look at the documentation for file_exists again. It does not take a file handle as an argument, it takes a string filename. The same is true for is_writable. Even if it did, you are opening the file handle and then immediately closing it, so I'm not sure why you're trying to use the file pointer at all after it's been closed.
You may be correct in that you have improper permissions set, but I would start here, first.
Also, if you're only trying to create the file, you may look into using the touch method, instead:
if( touch( $filename ) ) {
// It worked!
} else {
// It didn't work...
}
The web server is not executing as your user. touch /home/jack/Documents/cookie && chmod 777 /home/jack/Documents/cookie to allow the web server user to access the file.
Note this is BAD in production environments.
It looks like a permission issue. What user is PHP running as? It's likely running as www-data or something similar. You should make sure that the folders you are trying to write to are writable by either the user or group that PHP is running as. If you created those folders while logged in a jack, they probably belong to jack:jack and are not accessible by www-data:www-data.
You can also add jack to the www-data group, to make things a bit easier for development.

Where does file_put_contents save the files to?

I have read the documentation and it doesn't seem to indicate where can I expect the file to be created. I assumed that If I used file_put_contents on a server then a txt file would be created in the same place where the php file running it is. What am I missing here? I'm trying to save the url's in a text file as well.I just need them on my computer really not on the server.
while ($blekr<=$blekko_count)
{
echo '<a href='.$Blekko[$blekr]['url'].'><h4>'.$Blekko[$blekr]['url_title'].'</h4></a>';
echo '<p>'.$Blekko[$blekr]['snippet'].'<p>';
echo '<b>'.$Blekko[$blekr]['engine'].'</b>';
$file = 'Blekko.txt';
file_put_contents($file, $Blekko[$blekr]['url'], FILE_APPEND);
echo '<hr>';
$blekr++;
}
Unless you specify a different directory (using one or more slashes), the file is saved in the current working directory. getcwd() returns the current working directory; chdir() changes it.

Unlink() not working

I am trying to delete a file from the Server.
The files of my application is in a folder name "/public_html/app/";
All the images associated with the application is located in the following path: "/public_html/app/images/tryimg/"
The file in which I am writing the below code spec is in "/public_html/app/".
Here is my code snipet:
<?php
$m_img = "try.jpg"
$m_img_path = "images/tryimg".$m_img;
if (file_exists($m_img_path))
{
unlink($m_img_path);
}
// See if it exists again to be sure it was removed
if (file_exists($m_img))
{
echo "Problem deleting " . $m_img_path;
}
else
{
echo "Successfully deleted " . $m_img_path;
}
?>
When the above script is executed the message "Successfully deleted try.jpg" is displayed.
But when I navigate to the folder, the file is not deleted.
Apache: 2.2.17
PHP version: 5.3.5
What am I doing wrong?
Do I have to give a relative or absolute path to the image?
you're missing a directory separator:
$m_img = "try.jpg"
$m_img_path = "images/tryimg".$m_img;
// You end up with this..
$m_img_path == 'images/tryimgtry.jpg';
You need to add a slash:
$m_img_path = "images/tryimg". DIRECTORY_SEPARATOR . $m_img;
You also need to change your second file_exists call as you're using the image name and not the path:
if (file_exists($m_img_path))
You check the wrong path:
if (file_exists($m_img))
while you (tried to) delete(d) $m_img_path, so replace your check with
if (file_exists($m_img_path))
unlink() returns a boolean value to indicate whether the deletion succeeded or not, so it is easier/better to use this value:
if (file_exists($m_img_path))
{
if(unlink($m_img_path))
{
echo "Successfully deleted " . $m_img_path;
}
else
{
echo "Problem deleting " . $m_img_path;
}
}
Furthermore, the current directory is at the location where the script is executed, so you need to keep this in mind when using a relative path. In most situations it is probably better/easier to use absolute paths if possible.
If you need paths to a lot of files on your server, you might want to put the absolute path in a variable and use that, so it is easy to change the absolute location if your server configuration changes.

Categories