I want to move example.txt file on my server (/var/www/tmp) to different destination (/var/www/tmp/dir). I want to have this file only in one destination in which is file moved, so only one file on server. I have tried many commands but I wasn't sucessful. Can you help me with this issue? Thanks.
Source code:
<html>
<body>
<?php
// variables from the form
$blogentry = $_POST['blogentry'];
// creating or opening the file in append mode
$dataFile = "example.txt";
$fh = fopen($dataFile, 'a');
// writing to the file
fwrite($fh,"". " " . $blogentry . " " . "\n\n");
fclose($fh);
?>
</body>
</html>
You would use PHP's rename() function to move files (the name of the function can be a little confusing).
rename("/var/www/tmp/file.txt", "/var/www/tmp/dir/file.txt");
You can probably just use the rename() function. From http://php.net/manual/en/function.rename.php:
rename("var/www/tmp/example.txt", "/var/www/tmp/dir/example.txt");
Related
I'm using the following code to display the name of a CSV file before the contents of the CSV file are displayed on a PHP page. It works well and displays the name of the file as it should:
CODE:
#$result .= '<h3>'.$prog.'</h3>'.PHP_EOL; // OLD STYLE
$result .= '<h3>'.$prog. ' '.$_POST['filename'].'</h3>'.PHP_EOL; // NEW STYLE
$result .= '</div>'.PHP_EOL;
$result .= $contents.PHP_EOL;
Problem is, I don't want the file name to display the extension (in my code example, it is .csv)
Can anyone assist in the right code needed to remove the extension (.CSV)...?
// Use this code I hope it's useful ..
=> The basename() function returns the filename
<?php
$path = "filename.CSV"; // set your file name
//Show filename with file extension
echo basename($path) ."<br/>";
//Show filename without file extension
echo basename($path,".CSV");
?>
OutPut :-
filename.CSV
filename
OR
// you also use pathinfo() function .
$file_name = pathinfo('filename');
echo $file_name['dirname'], "\n";
echo $file_name['basename'], "\n";
echo $file_name['extension'], "\n";
echo $file_name['filename'], "\n"
==> Check this Demo Link :-
https://eval.in/930790
I have a script with a mysql query which saves a file called invoice.xml every day automatically by running a cron job. In case no data is found a no_orders.txt is saved.
I would like this file not be saved to the same folder as the script.php file is in but to a subfolder called invoices.
The renaming of the old invoice.xml is done with the following code
// rename old file
$nowshort = date("Y-m-d");
if(file_exists('invoice.xml')) {
rename('invoice.xml','invoice_'.$nowshort.'.xml');
}
The saving is done with the following code:
if($xml1 !='') {
$File = "invoice.xml";
$Handle = fopen($File, 'w');
fwrite($Handle, $xml1);
print "Data Written - ".$nowMysql;
fclose($Handle);
#print $xml;
die();
} else {
print "No new orders - ".$nowMysql;
$File = "no_orders_".$nowshort.".txt";
$Handle = fopen($File, 'w');
fclose($Handle);
die();
}
Could I please get assistance how to save this file to a subfolder. Also the renaming of the existing file would need to be within the subfolder then. I have already tried with possibilities like ../invoice/invoice.xml but unfortunately without any success.
Thank you
Just give the path of file 'invoice.xml' to $File.
Otherwise create some $Dir object which will point to Folder named 'invoice', then use accordingly
Use __DIR__ magic constant to retrieve your script.php directory, then you can append /invoice/invoice.xml .
Example if path to your script php something like this:
/var/www/path/to/script.php
$currentDir = __DIR__; //this wil return /var/www/path/to
$invoicePath = $currentDir.'/invoice/invoice.xml';
I am trying to copy a file that I download it. The file name is test1234.txt, but I want to access it using a wildcard like this: test*.txt and after that to move it to another folder (because I don't know how the file name looks like, but I know that the beginning is test and the rest is changing every time I download a new one). I tried some codes:
$myFile = 'C:/Users/Carl/Downloads/'. date("y-m-d") . '/test*.txt';
$myNewFile = 'C:/Users/Carl/Downloads/'. date("y-m-d").'/text.xml';
if(preg_match("([0-9]+)", $myFile)) {
echo 'ok';
copy($myFile, $myNewFile);
}
I am getting an error because of * in $myFile. Any help is very appreciated.
$myFile= 'C:/Users/Carl/Downloads/'. date("y-m-d") . '/test*.txt';
$myNyFile = 'C:/Users/Carl/Downloads/'.date("y-m-d").'/test.txt';
foreach (glob($myFile) as $fileName) {
copy($fileName, $myNyFile);
}
For complete response, if you want to only move *.txt in NewFolder.
$myFiles = 'C:/Users/Carl/Downloads/*.txt';
$myFolderDest = 'C:/Users/Carl/NewFolder/';
foreach (glob($myFiles) as $file) {
copy($file, $myFolderDest . basename($file));
}
I have a form(HTML, PHP) that lets the end user upload a file to update the database(MySQL) with the records in the uploaded file(specifically .csv). However, in the phpscript, I can only get the filename and not the complete path of the file specificed. fopen() fails due to this reason. Can anyone please let me know how I can work on finding the complete path?
HTML Code:
<html>
<head>
</head>
<body>
<form method="POST" action="upload.php" enctype="multipart/form-data">
<p>File to upload : <input type ="file" name = "UploadFileName"></p><br />
<input type = "submit" name = "Submit" value = "Press THIS to upload">
</form>
</body>
</html>
PHP Script:
<?php
.....
......
$handle = fopen($_FILES["UploadFileName"]["name"], "r"); # fopen(test.csv) [function.fopen]: failed to open stream: No such file or directory
?>
name refers to the filename on the client-side. To get the filename (including the full path) on the server-side, you need to use tmp_name:
$handle = fopen($_FILES["UploadFileName"]["tmp_name"], 'r');
$target='uploads/'.basename($_FILES['UploadFileName']['name']);
if(move_uploaded_file($_FILES['UploadFileName']['tmp_name'],$target)) {
//Insert into your db
$fp = fopen($target, "r");
}
I wrote like this:
$filePath = realpath($_FILES["file"]["tmp_name"]);
This gave me the full path to the uploaded file in PHP. If you find 0 bytes problem in file download, just modify this content-lenght line like this
header("Content-Length: ".filesize($filePath));
Where $filePath should be absolute path to file not just file handle.
Use the following code,
$handle = fopen($_FILES["UploadFileName"]["tmp_name"], 'r');
I use like this...
<?php
$NameOriginal = $_FILES["UploadFileName"]['name'];
$Typo_Image = $_FILES["UploadFileName"]['type'];
$name_Temp = $_FILES["UploadFileName"]['tmp_name'];
?>
I have this code to copy an html file and rename it. However it doesn't do this, and I have tried tons of variations of the code but still nothing. I'm probably just overlooking something, or I forgot something.
$file = 'example.html';
$newfile = '$bla.html;
Any ideas on how to fix this? Or a different code? Thanks in advance!
All you're doing here is creating variables, you have to actually copy the file. Check out PHP's copy() function.
Here's an example of how to use it:
$file = 'example.txt'; //path to source file, not just the filename
$newfile = 'example.txt.bak'; //same for this string as above
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
http://php.net/manual/en/function.copy.php
Alternatively you can do
`$file = 'example.html';
$newfile = 'bla.html;
file_get_contents($file);
file_put_contents($file,$newfile);`