Forum Moderators: coopster

Message Too Old, No Replies

Edit a copy of the file on server and create a unique link

I don't know php, just trying to study things, don't yell on me

         

It Never Works

11:33 pm on Dec 17, 2011 (gmt 0)

10+ Year Member



Hi, all.


As I asked , don't yell on me, pls, just a very beginner.


I need to create a PHP script to make 4 things.
It will work with jQuery POST script.
jQuery will transmit 4 to 5 strings: st1, st2... , and name of the editable file to PHP.

PHP have to:
1. Create a copy a file to some location (can be same folder), can be done like that.

<?php
$file = 'example.txt';
$newfile = 'exampleTIMETAG.txt';

if (!copy($file, $newfile)) {
echo "Ooops.. Didn't work! ";
}
?>




2. Find a specific lines (can be some "CHANGE_ME1" words).

I don't know, and can't find how to do it...
Need help here.


3.Create a download link.
$newfile will be returned to jQuery, it will handle it (no problem here).


4.Delete $newfile from server after the download (or 5 minutes)

I onClick function can send a delete signal to PHP after some deley (file is about 100kb MAX), or some timer will run trigger the function.
Need help with it.



If some one willing to help, or have some suggestions,or even links to useful posts, I will be grateful.

lostdreamer

10:34 am on Dec 19, 2011 (gmt 0)

10+ Year Member



Let jquery post the data to download.php

download.php:

$arrFiles = array(
// put legal files in here
"test.txt",
"example.txt",
);
if(isSet($_POST['file']) && file_exists($_POST['file']) && ( array_search($arrFiles, $_POST['file']) !== false )) {
$newFile = $_POST['file'] ."-". date("y-m-d His") .".txt";

// copy the file (Why do you want this anyway ?)
if (!copy($_POST['file'], $newFile))
exit("Could not copy the file");

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($newFile));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($newFile));
ob_clean();
flush();
readfile($file);
// delete file when it has been output to the client....
unlink($newFile);
exit;
}


Something like this should help you on the way....
This way you don't need to think about "How long will the download take before deleting the file...."

When you use jquery to post te data to download.php the download will also start...

Good luck,
LostDreamer

It Never Works

10:55 pm on Dec 20, 2011 (gmt 0)

10+ Year Member



lostdreamer, that is a grate code!
Almost what i wanted! TNX!

after this part:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($newFile));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($newFile));
ob_clean();
flush();
readfile($file);
// delete file when it has been output to the client....
unlink($newFile);
exit;
}

I really don't need to copy anything. master piece!

working ow on str_replace() , and arrays now.
That was a huge help never would find it myself.
Thank you.