PHP List Content of .txt files from folder [closed] - php

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
On my website, I have an upload story feature with a user written title and story. It uploads it as a text file in a directory. Is there a way to list the content of these files using php or anything? Also, I'd like to only show like 200 chars of the story, and have a 'show full story' button, that would show the full story (I'll use jQuery for this).
Thanks!

$dataArray = array();
//Number of chars for the string
$num = 200;
//Check if DIR exists
if ($handle = opendir('.')) {
//Loop over the directory
while (false !== ($file = readdir($handle))) {
//Strip out the . and .. files
if ($file != "." && $entry != "..") {
$dataArray[] = array();
//Store file contents
$filecontent = file_get_contents($file);
//Split the content and store in array
$length = strlen($filecontent);
$dataArray[] = array(substr($filecontent, 0, $num), substr($filecontent, $num, $length ));
}
}
//close the dir
closedir($handle);
}
With this you get an array with all the content of your .txt files, splittet into 2 Strings, one with 200 chars the other with the rest.
The string with 200 length is $dataArray[x][0] and the other is $dataArray[x][1].
Now you can use this in HTML:
<?php foreach($dataArray as $data) { ?>
<div class="visible">
<?php echo $data[0]; ?>
</div>
<div class="hidden">
<?php echo $data[1]; ?>
</div>
<?php } ?>

in php.net:
to open a directory:
opendir() http://php.net/manual/en/function.opendir.php
$dir = opendir('/path/to/files');
to read the directory (you can loop):
readdir() http://www.php.net/manual/en/function.readdir.php
while (false !== ($file= readdir($dir))) {
//$file has the filename
}
to get the content of a file:
file_get_contents() http://php.net/manual/es/function.file-get-contents.php
$content=file_get_contents($file);

Related

Cannot remove images from directory [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have written these scripts to delete files (image, in fact) in a directory but have the choice to decide which one to delete and view before hand. The view.php script seems to be working fine, however the delete.php doesn't seem to be functioning because the images are not removing.
here are the scripts:
view.php
<?php
$path = '../product-uploads/gloves/'; // path for each page
$files = glob("{$path}*.*"); // Get the files
$files = array_filter($files, 'is_file'); // Get rid of directories
$dates = array_map('filectime', $files); // Get the creation times.
$md5s = array();
array_multisort($dates, $files, SORT_NUMERIC); // in order of creation
foreach ($files AS $file)
{
$hash = md5_file($file);
if (!in_array($hash, $md5s))
{
$md5s[] = $hash;
echo "<img src=\"$file\" /> <br />
<form action=\"delete.php\" method=\"post\">
<input type=\"hidden\" name=\"Name\" value=\"$file\">
<input type=\"submit\" value=\"Delete\">
</form>";
}
}
?>
delete.php
<?php
$path = '../product-uploads/gloves/';// images are here
$Name = $_POST['Name'];
$PathFile = $path.$Name;
$PathFile = basename($PathFile);
header('Location: view.php');
?>
This is because you don't remove the images in your delete.php
Use unlink on pathfile.

How to Secure php picture upload system [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to make picture upload system with php and i want users will be able to upload just
png,jpg files and not viruses or other files. how can i do this?
($_FILES["file"]["type"] == "image/png")
for more information about this click here
step 1: Check the extension (extension file ends with)
step 2: Check the MIME type ($file_info = getimagesize($_FILES['image_file']; $file_mime = $file_info['mime'];)
only allow the those image extension which you want to upload , for that you can make the white list
try something like
$whitelist = array(".jpeg",".jpg",".png");
foreach ($whitelist as $item)
{
if(preg_match("/$item\$/i", $_FILES['uploadfile']['name']))
{
$uploaddir='uploads/uploads_image/';
$uploadfilename=mysql_prep(basename($_FILES['uploadfile']['name']));
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv= mcrypt_create_iv($iv_size,MCRYPT_DEV_RANDOM);
$newname= mcrypt_encrypt(MCRYPT_RIJNDAEL_256, "this is the key",$uploadfilename.time(), MCRYPT_MODE_ECB, $iv);
$newfilename= (bin2hex($newname));
$uploadfile=$uploaddir.$newfilename.".png";
$access=true;
}
}
you can also block the ip of the user if user try to upload miscellaneous file by making the black list
foreach ($blacklist as $item)
{
if(preg_match("/$item\$/i", $_FILES['uploadfile']['name']))
{
$network = ip2long("10.12.0.0");
$mask = ip2long("255.255.0.0");
$ip = ip2long($_SERVER{'REMOTE_HOST'});
if (($network & $mask) == ($ip & $mask)) {
die("Unauthorized");
}
}
}
1st check:-
//check if contain php and kill it
$pos = strpos($filename,'php');
if(!($pos === false)) {
die('error');
}
2nd check:-
//get the file ext
$file_ext = strrchr($filename, '.');
$image_list = array(".jpg",".jpeg",".gif",".png");
if (!(in_array($file_ext, $image_list))) {
die('not allowed extension,please upload images only');
}
3rd check:-
$fileType = $_FILES["uploaded_file"]["type==image/jpeg || image/gif || image/png"];
4th check:-
preg_match("/.(gif|jpg|png)$/i", $fileName);
hope these checks solve your problem....

PHP to ASP.NET / C# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to convert the following to ASP.NET C#
<?php
/* Configuration Start */
$thumb_directory = 'img/thumbs';
$orig_directory = 'img/original';
$stage_width=600; // How big is the area the images are scattered on
$stage_height=400;
/* Configuration end */
$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;
/* Opening the thumbnail directory and looping through all the thumbs: */
$dir_handle = #opendir($thumb_directory) or die("There is an error with your image directory!");
$i=1;
while ($file = readdir($dir_handle))
{
/* Skipping the system files: */
if($file=='.' || $file == '..') continue;
$file_parts = explode('.',$file);
$ext = strtolower(array_pop($file_parts));
/* Using the file name (withouth the extension) as a image title: */
$title = implode('.',$file_parts);
$title = htmlspecialchars($title);
/* If the file extension is allowed: */
if(in_array($ext,$allowed_types))
{
/* Generating random values for the position and rotation: */
$left=rand(0,$stage_width);
$top=rand(0,400);
$rot = rand(-40,40);
if($top>$stage_height-130 && $left > $stage_width-230)
{
/* Prevent the images from hiding the drop box */
$top-=120+130;
$left-=230;
}
/* Outputting each image: */
echo '
<div id="pic-'.($i++).'" class="pic" style="top:'.$top.'px;left:'.$left.'px;background:url('.$thumb_directory.'/'.$file.') no-repeat 50% 50%; -moz-transform:rotate('.$rot.'deg); -webkit-transform:rotate('.$rot.'deg);">
<a class="fancybox" rel="fncbx" href="'.$orig_directory.'/'.$file.'" target="_blank">'.$title.'</a>
</div>';
}
}
/* Closing the directory */
closedir($dir_handle);
?>
In ASP.NET how do i loop through a series of images from a directory?
What does "explode" mean in PHP?
Is this a do while loop?
This may help you. It has complete code samples:
How to: Enumerate Directories and Files
http://msdn.microsoft.com/en-us/library/dd997370.aspx

Find and Replace a string in more than one text file in PHP [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
i found this code:
<?php
$path_to_file = 'c:\wamp\www\FindReplace\File.txt';
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace("Paul",",HHH",$file_contents);
file_put_contents($path_to_file,$file_contents);
?>
It's working very well if it's only one file but how to do if i want to Find and Replace in all *.txt file of my folder?
THank you
What about this?
It should take care of running your replace on all .txt-files, regardless of how many sub-folders you got in your path folder.
$path = realpath(__DIR__ . '/textfiles/'); // Path to your textfiles
$fileList = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::SELF_FIRST);
foreach ($fileList as $item) {
if ($item->isFile() && stripos($item->getPathName(), 'txt') !== false) {
$file_contents = file_get_contents($item->getPathName());
$file_contents = str_replace("Paul",",HHH",$file_contents);
file_put_contents($item->getPathName(),$file_contents);
}
}
find /path/to/your/project -name '*.txt' -exec php yourScript.php {} \;
Then modify your script to use command line argument $argv[1] as the file path.
You can use useful glob(), internal PHP function
$files_in_your_folder = glob('c:\wamp\www\FindReplace\*');
So for your specific case:
<?php
foreach(glob('c:\wamp\www\FindReplace\*') as $path_to_file) {
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace("Paul",",HHH",$file_contents);
file_put_contents($path_to_file,$file_contents);
}
?>

Directory tree listing [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Hi i am looking for script witch lists directory's from ftp and read's all files from the listed folder..Is there something that can help me?
> //open the dir
$sub = ($_GET['dir']);
$path = 'store/'.$diro.'/';
$path = $path . "$sub";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh)) !== false) {
if($file != "." && $file != "..") {
if (substr($file, -4, -3) =="."){
echo "$i. $file ";
}else{
echo "$i. $file";
}
$i++;
}
}
closedir($dh);
I did a quick Google, and there a MILLIONS of responses to this question.
The fourth result is a complete code sample.
http://www.phpreg.com/index.php?option=com_content&task=view&id=158&Itemid=28
Something like....
<?php
$connect = ftp_connect("ftp.hostname.com");
$result = ftp_login($connect, "username", "password");
$a = ftp_nlist($connect, "code22");
foreach($a as $value){
echo $value, "<BR>";
}
?>

Categories