create file in another directory with php - php

My folder structure is like -
root
admin
create_page.php
pages
my_page1.php
my_page2.php
I have code for creating a new php file in "pages" folder. the code is like -
$dir_path = "../pages/";
$ourFileName = '../'.$page_name.".txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
$ourFileContent = '<?php echo "something..." ?>';
if (fwrite($ourFileHandle, $ourFileContent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
The code executes normally..no problem. but the page is not being created. please tell me what i am doing wrong. is there problem with the path?
fclose($ourFileHandle);

Here's an example using the more simpler file_put_contents() wrapper for fopen,fwrite,fclose
<?php
error_reporting(E_ALL);
$pagename = 'my_page1';
$newFileName = './pages/'.$pagename.".php";
$newFileContent = '<?php echo "something..."; ?>';
if (file_put_contents($newFileName, $newFileContent) !== false) {
echo "File created (" . basename($newFileName) . ")";
} else {
echo "Cannot create file (" . basename($newFileName) . ")";
}
?>

$ourFileHandle = fopen("../pages/" .$ourFileName, 'w') or die("can't open file");
Make the above change to the third line and it will probably work; I tried it and it worked.

have you deliberately missed out on concatinating $dir_path and $ourFileName;
now
is your directory/file writable?
check for your current working directory
is your error_reporting on ?

Had the same Problem, File will not be created in a folder having different permission, make sure the permissions of the folders are same like other files.

Try this
$pagename = 'your_filename';
$newFileName = '../pages/'.$pagename.".php";

Related

How to copy an image from one folder to another using php

I'm having difficulty in copying an image from one folder to another, now i have seen many articles and questions regarding this, none of them makes sense or work, i have also used copy function but its giving me an error. " failed to open stream: No such file or directory" i think the copy function is only for files. The image i wanna copy is present in the root directory. Can anybody help me please. What i am doing wrong here or is there any other way???
<?php
$pic="somepic.jpg";
copy($pic,'test/Uploads');
?>
You should write your code same as below :
<?php
$imagePath = "/var/www/projectName/Images/somepic.jpg";
$newPath = "/test/Uploads/";
$ext = '.jpg';
$newName = $newPath."a".$ext;
$copied = copy($imagePath , $newName);
if ((!$copied))
{
echo "Error : Not Copied";
}
else
{
echo "Copied Successful";
}
?>
You should have file name in destination like:
copy($pic,'test/Uploads/'.$pic);
For your code, it must be like this:
$pic="somepic.jpg";
copy($pic,'test/Uploads/'.$pic);
Or use function, like this:
$pic="somepic.jpg";
copy_files($pic,'test/Uploads');
function copy_files($file_path, $dest_path){
if (strpos($file_path, '/') !== false) {
$pathinfo = pathinfo($file_path);
$dest_path = str_replace($pathinfo['dirname'], $dest_path, $file_path);
}else{
$dest_path = $dest_path.'/'.$file_path;
}
return copy($pic, $dest_path);
}

How to create file in special directory using PHP

How can I create a file on the server in the directory I want using php? I found the function fopen but it won't help me, because we cannot choose the directory.
Example:
$href = '/contact/downloads/227856/file2.php'
if (file_exists($href)) {
echo "File exists";
} else {
echo "File doesn't exist, but we will create it";
// And there we gonna create the file in the directory $href
}
$path_to_file = " " //Make sure $path_to_file is the full path, something like /home/user/public_html/etc/ <br>
$yourFileName = 'myfile';
$ourFileHandle= fopen($path_to_file.$yourFileName, 'w') or die('Permission error');

PHP, Creating File in Directory

Help!
So I've look at many solutions of this on stack overflow, but non seem to have worked. So what I'm doing is creating a directory with a random number, then creating a text file in it.
My directory is like this:
localhost
--/tdir/
---/(random number directory) < this is were i want to save to the text file
Here is my code:
<?php
$dir = rand(1, 1999999);
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
if ($dir == true) {
echo "Created directory! :D";
} else {
echo "Failed to create directory! ~ :(";
}
chmod("/tdir/$dir/", 0777);
echo "</br><a href='/tdir/'>Go Back</a>";
echo "</br><a href='/tdir/$dir'>Go to page!</a></br>";
$my_file = '../'.$dir.'/file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
$data = 'This is the data';
fwrite($handle, $data);
?>
I keep getting this error:
Created directory! :D
Go Back
Go to page!
Cannot open file: ../1320710 *random number directory* /file.txt
I need to be able to save to file.txt in the $dir directory!
You have to evaluate the response of the mkdir function, just write it like this
if (mkdir($dir, 0777, true) === TRUE){
....
Also, when writing/reading folders, make sure that you use System path, not URL path.

failed to open stream: No such file or directory

Can anyone help with this one? I am new to web developing and not sure what this error means?
Warning: fopen(images/nophoto.png): failed to open stream: No such
file or directory in /home/u835626360/public_html/remove.html on line
101
can't this file/picture is open you need close
CODE:
$expire=time()-3600;
setcookie("dname","a", $expire);
setcookie("dpode","a", $expire);
}
function delpics($filename)
{
$path_to_file='userpics/';
$old = getcwd(); // Save the current directory
chdir($path_to_file);
$fh = fopen($filename, 'w') or die("can't this file/picture is open you need close ");
fclose($fh);
if (!unlink($filename))
{
echo ("Error deleting $file");
}
else
{
echo ("Deleted $filename");
}
chdir($old); // Restore the old working directory
}
You need to give fopen the full path of the file, and you don't need chdir() at all. Try this version:
$path_to_file='userpics/';
$fh = fopen($path_to_file.$filename, 'w') or die('Permission error');
I was facing same problem. I was thinking file will be created if I use w or w+ but giving me above error.
So problem was we need to create dir before we can create file.
We can get absolute DIR path of file
$dir = dirname($filename);
Create DIR
//if(!is_dir($dir))
mkdir( $dir , 0755, true);
Third parameter is important you want recursively
I know it sound stupid but it may save someone's time so added here
First make the dir manually in your server(if you have one) or local pc(if you dev in local)
Be sure to have write right for apache in your dir (0777 in unix-linux if you wan't to be sure you can do what you wan't and no idea for windows)
and then like it was said give the good path to fopen and not only filename
Try this:
$expire=time()-3600;
setcookie("dname","a", $expire);
setcookie("dpode","a", $expire);
function delpics($filename)
{
$path_to_file='/userpics/';
$old = getcwd(); // Save the current directory
chdir($old.$path_to_file);
$fh = fopen($filename, 'w') or die("can't this file/picture is open you need close ");
fclose($fh);
if (!unlink($filename))
{
echo ("Error deleting $file");
}
else
{
echo ("Deleted $filename");
}
chdir($old); // Restore the old working directory
}

How to auto delete a zip file after using stream_get_contents(); to view a file in php?

I have this code to read a file for preview, but the downside is I have to download the file first from cloud and read from it, but it's a waste of space so I want to delete it after viewing a certain file. Is there an automatic way of doing this? Or do I have to integrate it to a close button?
// Get the container we want to use
$container = $conn->get_container('mailtemplate');
//$filename = 'template1.zip';
// upload file to Rackspace
$object = $container->get_object($filename);
//var_dump($object);
//echo '<pre>' . print_r($object,true) . '</pre>';
$localfile = $dir.$filename;
//echo $localfile;
$object->save_to_filename($localfile);
if($_GET['preview'] == "true")
{
$dir = "../mailtemplates/";
$file1 = $_GET['tfilename'];
$file = $dir.$file1;
$file2 = "index.html";
$info = pathinfo($file);
$file_name = basename($file,'.'.$info['extension']);
$path = $file_name.'/'.$file2;
$zip = new ZipArchive();
$zip->open($file);
$fp = $zip->getStream($path);
if(!$fp)
{
exit("faileds\n");
$zip->close();
unlink($dir.$filename);
}
else
{
$stuff = stream_get_contents($fp);
echo $stuff;
$zip->close();
if($stuff != null)
{
unlink($dir.$filename);
}
}
}
else
{
unlink($dir.$filename);
}
You didn't google this did ya?
Try Unlink
Edit:
Taking a look at this code, $zip->open($file); <-- is where you open the file. The file variable is set by:
"../mailtemplates/" . basename($_GET['tfilename'], '.' . $info['extension']) . '/' . "index.html"
So you're grabbing a relative directory and grabbing a filename as a folder, and going to that folder /index.html. Here's an example:
if you're in c:\ testing and you go to ../mailtemplates/ you'll be in c:\mailtemplates and then you're looking at file test.php but you're removing the file extension, so you'll be opening the location c:\mailtemplates\test\index.html so you open up that html file and read it. Then, you're trying to delete c:\mailtemplates\test.php
can you explain how any of that makes sense to you? 'cause that seems very odd to me.

Categories