I've got a script which lists all the txt files that I have in a directory. I've put a hyperlink each of the results that comes up and I want to the redirect to a simple editor which I have the code for.
This is the code for listing the files in the directory;
<?php
//Open directory
$dir = dir("content");
//List files in directory
while (($file = $dir->read()) !== false){
//Make sure it's a .txt file
if(strlen($file) < 5 || substr($file, -4) != '.txt')
continue;
echo "Page: <a href='content/" .$file. "'>" . $file . "</a><br />";
}
$dir->close();
?>
And the code to edit the file:
<?
if($_POST['Submit']){
$open = fopen("content/body.txt","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />";
echo "File:<br />";
$file = file("content/body.txt");
foreach($file as $text) {
echo $text."<br />";
}
}else{
$file = file("content/body.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
}
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>
What's the best way to try and do this?
Thanks in advance.
I think you mean this:
in directory list use this:
echo "Page: <a href='editor_script.php?filename=".$file."'>".$file."</a><br/>";
in your editor script use this:
$open = fopen("content/".$_GET['filename'].".txt","w+");
$file = file("content/".$_GET['filename'].".txt");
I' not sure but maybe you have to remove action from form
echo '<form method="post">';
or use url with $_GET['filename']
echo '<form action="editor_script.php?filename='.$_GET['filename'].'" method="post">';
maybe this works too but I'm not sure
echo '<form action="?filename='.$_GET['filename'].'" method="post">';
Related
So I have look through and directory exclude certain files, show files numbered with a delete button and need to write in a delete function to delete the directory and the files in it and not sure how that is written can someone provide an answer.
<?php
$dir = './';
$files = scandir($dir);
sort($files);
$count = -1 ;
foreach ($files as $file) {
$v_delete = "delete_".$count;
if ($file != '.' && $file != '..' && $file != 'read.php') {
$str_URL = "./".$file;
echo "<tr>";
echo "<td>";
echo $count;
echo "</td>";
echo "<td>";
echo $file;
echo "</td>";
echo "<td>";
echo "<form action='' method='post'><input type='submit' value='Delete' name='".$v_delete."'/></form>";
if(isset($_POST[$v_delete])) {
// Your php delete code here
echo "delete file : ".$file;
}
echo "</td>";
}
$count++;
}
?>
I have a project where a user uploads a photo with a name and caption, and I am having trouble with displaying the name and caption.
Right now, it's displaying the entire text file with each image, and I am having trouble fixing this.
My code so far:
<?php
$Dir = "files";
$DirEntries = scandir($Dir);
foreach ($DirEntries as $Entry)
{
if((strcmp($Entry, '.') != 0) && (strcmp($Entry, '..') != 0))
{
echo "<p>Name: " . file_get_contents("imagelist.txt") . "</p>";
echo "<a href=\"files/" . $Entry . "\" target=\"_blank\" >" . $Entry . "</a><br />\n";
}
}
closedir($DirOpen);
?>
Any help would be greatly appreciated.
You can use fgets():
$inputFile = fopen("file.txt", "r");
if ($inputFile) {
while (($line = fgets($inputFile)) !== false) {
echo $line."<br>";
// The process read the file line by line
}
} else {
echo "There was an error in the opening file";
}
fclose($inputFile);
I have a script that uploads and display a CSV file into HTML table.
My problem is that I have an error when I try to open big files.
My file is about 4.5 Mo with 80000 lines. It works fine with small files but I get
Warning: fopen(upload/test.csv): failed to open stream: No such file or directory in /var/www/cvs/PHP charts/sb-admin-v2/test.php on line 45
with big files
Here is my code
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" name="submit" />
</form>
<?php
//upload
// for set memory limit & execution time
ini_set('memory_limit', '512M');
ini_set('max_execution_time', '180');
if ( isset($_POST["submit"]) ) {
if ( isset($_FILES["file"])) {
//if there was an error uploading the file
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else {
//if file already exists
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
}
else {
//Store file in directory "upload" with the name of "uploaded_file.txt"
$storagename = "test.csv";
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $storagename);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"] . "<br />";
}
}
} else {
echo "No file selected <br />";
}
}
//display
$row = 1;
if (($handle = fopen("upload/test.csv", "r")) !== FALSE) {
echo '<table border="1">';
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<thead><tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
//echo $data[$c] . "<br />\n";
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td>'.$value.'</td>';
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
?>
Well when I ran into this problem I resorted to a client side solution with https://github.com/knrz/CSV.js. Works very well and takes the load off my server. Only thing is I did not need the entire CSV to be uploaded to my server, once parsed only selective portions were sent using ajax.
If you are only trying to display the data in HTML table this can be a very good solution.
I'm looking for a way to do a function with php when clicking on a link.
I mean to delete an item when clicking on this link. I got my code set up like this:
$files = glob("upload/*.*");
if(count($files) == 0){
echo "No files present";
} else {
foreach ( $files as $file ) {
$array = explode("/", $file);
//echo $array[1] . "<br>";
echo "<tr>";
echo "<td>" . $array[1] . "</td>";
echo '<td><img height="70" width="auto" src=" ' . $file . '" ></td>';
echo '<td>Delete item</td>';
echo "</tr>";
}
}
In your main script:
$files = glob("upload/*.*");
if(!count($files)){
echo "No files present";
} else {
foreach ($files as $file) {
$array = explode("/", $file);
echo "<tr>";
echo "<td>" . $array[1] . "</td>";
echo '<td><img height="70" width="auto" src=" ' . $file . '" ></td>';
echo '<td>Delete item</td>';
echo "</tr>";
}
}
In your delete script:
if (isset($_GET['file']) && $file = rawurldecode($_GET['file'])) {
if (file_exists('upload/' . $file))
unlink($file);
}
You might want to pass in another $_GET parameter (secret token) for added security - otherwise it'll open up your uploads directory to permanent threat. You can send a random sha1() hash or system-defined configuration value, and have your delete script expect a 40-char string.
I want to show images residing inside a directory in PHP
The code which I wrote was
<?php
$dir = opendir("upload");
while (($file = readdir($dir)) !== false)
{
echo "filename: " . $file . "<br />";
echo "<a href='shoe.php?image_name=".$file."'> <img src='upload/".$file[0]."' width='150' height='150' /></a></td>";
}
closedir($dir);
?>
Here the images are not displayed. Only a frame comes for that image and the actual image is not displayed inside that.
Where I have went wrong? Can any one please help...
PHP Glob makes it much easier to list directories. You can do something like
$image = glob("*.jpg")
foreach ($image as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
Replace $file[0] with $file, as $file is not an array and that will only return the first character of the string.
why $file[0]??? $file is correct:
<?php
$dir = opendir("upload");
while (($file = readdir($dir)) !== false)
{
echo "filename: " . $file . "<br />";
echo "<a href='shoe.php?image_name=".$file."'> <img src='upload/".$file."' width='150' height='150' /></a>";
}
closedir($dir);
?>