Finding substring when reading from an uploaded file fails - php

Why can't I do a proper strpos function on an uploaded file (I first upload the file, and then do a $site = stream_get_line($f, 4096, "\n")) and it works completely fine when read the same thing into $site from a manually created file on the server.
I have no idea what's causing it... it seems to read the line well in both cases, but strpos just doesn't work the same when it's uploaded via user versus when I create it manually.
Here's the code:
<?php
if(!$_FILES){
?>
<head>
<title>Bulk Index Checker</title>
</head>
<center>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br/>
<input type="submit" value="Check 'em!" />
</form>
</center>
<?php
}
else{
echo("<center> <h1>Checking... please wait.</h1> <br><table border=1> <tr><td>Site</td><td>Indexed</td></tr>");
$target_path = "checkupload/";
$target_path = $target_path . basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $target_path);
$f = fopen($target_path, "r") or exit("Unable to open file!");
while (!feof($f))
{
$site = stream_get_line($f, 4096, "\n");
$url = 'http://www.google.com/search?q=info:' . $site;
//sleep(3);
$contents = file_get_contents($url);
if (strpos($contents,'<h3 class="r"><a href="/url?q='.$site)!=FALSE) {
echo("<tr bgcolor=\"Silver\"><td>".$site."</td><td>YES</td></tr>");
echo("<br>");
}
else{
echo("<tr><td>".$site."</td><td>NO</td></tr>");
}
}
?>
</table>
<br/>
<form>
</center>
<?php
}
?>
Edit: Here's another way I tried (using textarea) - same thing basically - it ONLY works properly when you input only 1 URL. As soon as you input another URL below, it shows a FALSE for the first one when it's really TRUE.
WHY IS THIS HAPPENING?
<?php
if(!$_POST){
?>
<head>
<title>Bulk Index Checker</title>
</head>
<center>
<h1>Bulk Index Checker v1.0</h1>
<form method="post" enctype="multipart/form-data">
<textarea id="list" name="list" rows="10" cols="50"></textarea>
<br/>
<input type="submit" value="Check 'em!" />
</form>
</center>
<?php
}
else{
echo("<center> <h1>Checking... please wait.</h1> <br><table border=1> <tr><td>Site</td><td>Indexed</td></tr>");
$lines = explode("\n", $_POST['list']);
foreach($lines as $site) {
echo($site);
$url = 'http://www.google.com/search?q=info:' . $site;
sleep(3);
$contents = file_get_contents($url);
if (strpos($contents,'<h3 class="r"><a href="/url?q='.$site)!=FALSE) {
echo("<tr bgcolor=\"Silver\"><td>".$site."</td><td>YES</td></tr>");
echo("<br>");
}
else{
echo("<tr><td>".$site."</td><td>NO</td></tr>");
}
}
?>
</table>
<br/>
</center>
<?php
}
?>

Try to use '!==' instead of '!='.
I don't have an option to test at this stage. See http://php.net/manual/en/function.strpos.php (example #2)

Related

Trouble Writing back to text file using PHP fwrite from file selected from a dropdown form

I am having trouble writing back to a text file selected from a html form with a dropdown menu, the script is able to read but is unable to write back, returns "Unable to Open File".using PHP fopen and fwrite
This is the index.html code:
<html>
<head></head>
<title>Edit Server Side Text Files</title>
<body>
<form action="function.php" method="post">
<select name="list">
<option value="./files/banned-kdf.txt">banned-kdf.txt</option>
<option value="./files/blackList.txt">blackList.txt</option>
</select>
<input type="submit" name="edit" value="Edit" />
</form>
</body>
</html>
And this is the function.php code:
<?php
if(isset($_POST["edit"])) {
$filename = $_POST["list"];
global $filename;
// var_dump($filename);
$myfile = fopen("$filename", "r") or die("Unable to open file!");
$filecontent = fread($myfile,filesize("$filename"));
fclose($myfile);
}
?>
<html>
<form action="<?php echo $PHP_SELF;?>" method="post">
<textarea name="textareaContent" rows="50" cols="100">
<?
if(isset($filecontent)) { echo $filecontent; }
?>
</textarea>
<br>
<br>
<input type="submit" name="update" value="Update" />
</textarea>
<?
//write to text file
if(isset($_POST["update"])) {
// The following two lines are just for troubleshooting and have been commented out.
// echo $_POST["update"];
// var_dump($filename);
$myfile = fopen("$filename", "w") or die("Unable to open file!");
fwrite($myfile, $_POST["textareaContent"]);
fclose($myfile);
}
?>
</form>
<br>
<br>
Return to List Selection
</html>
You're not sending file name in the second form, just code, so, $filename is undefined. Add it in an input hidden:
global $variable; is not useful here, because the variable will not be present on next script execution, when processing the form.
<?php
// Default value for contents:
$filecontent = '';
// Ok, read the file here
if(isset($_POST["edit"])) {
$filename = $_POST["list"];
global $filename; // This will not be useful
$myfile = fopen("$filename", "r") or die("Unable to open file!");
$filecontent = fread($myfile,filesize("$filename"));
fclose($myfile);
}
// If request comes from second form, process it
if(isset($_POST['update'])) {
// Get file name from input hidden
$filename = $_POST["filename"];
$myfile = fopen("$filename", "w") or die("Unable to open file!");
fwrite($myfile, $_POST["textareaContent"]);
fclose($myfile);
// End the script, no need to show the form again
die('File updated successfully.');
}
?>
<html>
<form action="<?php echo $PHP_SELF;?>" method="post">
<input type="hidden" name="filename" value ="<?php
// Send filename within form
echo $filename;
?>">
<textarea name="textareaContent" rows="50" cols="100">
<?
// No if needed here, we already have a value
echo $filecontent;
?>
</textarea>
<br>
<br>
<input type="submit" name="update" value="Update" />
</form>
<br>
<br>
Return to List Selection
</html>
Please check the permission of target file and the folder in this the file is supposed to be.

PHP Form seems to change action to a different script

I'm building a website that has a form that allows users to upload files, but the form seems to be going to the wrong place. I told it to go to "upload.php", but instead it tries to go to "“upload.php", which I did not include in the form action. It doesn't look like I added any unicode characters in the script. Any reason why this might be happening?
Here's my form in index.html:
https://pastebin.com/7PETk5Sy
<!DOCTYPE html>
<html>
<head>
<title>Deeper</title>
<link type="text/css" rel="stylesheet" href="style.css"/>
</head>
<body>
<div id="titlebar">
<img src="DeeperNetIcon.jpg"></img>
<h1>DeeperNet</h1>
Back to Deeper Homepage
<br/><br/>
<form action="view.php" method="post">Search for World by ID: <input type="text" name="world"> <input type="submit" value="Search"></form>
<br/>
</div>
<p color="white">Welcome to DeeperNet!</p>
<div id="upload">
<h1>Upload World to DeeperNet</h1>
<form action=“upload.php” method="post" enctype="multipart/form-data">
World File:
<br/><br/>
<input type="file" name="world">
<br/><br/>
World Name:
<br/>
<input type="text" name="name">
<br/>
Description:
<br/>
<input type="textbox" name="desc">
<br/><br/>
<input type="submit" value="Upload World">
</form>
<br/>
</div>
</body>
The index.html File has 2 Forms, the second form is the one I'm talking about.
Here's my upload.php Script:
https://pastebin.com/0FyuAX3Q
<?php
include("index.html");
if(!empty($_POST)) {
if(isset($_FILES["world"])) {
$world = $_FILES["world"];
$name = $world["name"];
$tmp_name = $world["tmp_name"];
$size = $world["size"];
$ext = explode('.', $name);
$ext = strtolower(end($ext));
$allowed = "deep";
if ($ext == $allowed) {
if ($size <= 300000) {
$id = uniqid('', true);
$destination = 'worlds/' . $id . '.' . $ext;
if(move_uploaded_file($tmp_name, $destination)) {
echo '<br/>World: "' . $name . '" uploaded to DeeperNet!';
echo “<br/>World ID: ” . $id;
$info = fopen('worlds/' . $id . '.txt', 'w');
fwrite($info, $_POST['name'] . "\r\n");
fwrite($info, $_POST['desc']);
fclose($info);
}
}
}
MAMP says I'm using PHP 7.0.8 if you're wondering.
Replace your code with this as form accepts this double quotes (") not this one (“)
<form action="upload.php" method="post" enctype="multipart/form-data">

Read and remove all html/css tags in content file (php)

I'm trying to develop a code to read a file and to remove the html and css tags in content, saving the modifications in this same file. Initially, I thought to use the fgetss function. But the function works in only 1/3 of file, and don't show others strings.
<?php
if(isset($_POST['send']))
{
$file = $_FILES["arq"]["name"];
$content = " ";
echo $file;
$pont = fopen($file, "r");
do {
$content = $content . " " .fgetss($pont);
} while(!feof($pont));
fclose($pont);
$pont2 = fopen($file, "w");
fwrite($pont2, $content);
fclose($pont2);
echo("fim");
}
?>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="arq" name="arq" value="">
<input type="submit" name="send" value="send"/>
</form>
</body>
And after, I developed other way, using strip_tags, but have been occurred the same error:
<?php
if(isset($_POST['send']))
{
$file = $_FILES["arq"]["name"];
$content = "";
$pont = fopen($file, "r");
do {
$content = $content. " " .fgets($pont);
} while(!feof($pont));
$content = strip_tags($content);
fclose($pont);
$pont2 = fopen($file, "w");
fwrite($pont2, $content);
fclose($pont2);
echo("fim");
}
?>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="arq" name="arq" value="">
<input type="submit" name="send" value="send"/>
</form>
</body>

how to find the length of the text file which user uploads

Guide me to find the number of characters which are present in the text file which i upload.
This code will display the characters which are present in the .txt file`
<!DOCTYPE html>
<html>
<head>
<title>File Reader</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="60" accept=".txt" />
<input type="submit" value="Show Contents" />
</form>
<?php
if ($_FILES) {
$fileName = $_FILES['file']['tmp_name'];
$file = fopen($fileName, "r");
while (!feof($file)) {
echo fgets($file) . "";
}
while (!feof($file)) {
echo fgetc($file);
}
fclose($file);
}
?>
</body>
</html>
You could use
$_FILES['userfile']['size']
this give you the size, in bytes, of the uploaded file.
http://www.php.net/manual/en/features.file-upload.post-method.php

Edit a file with PHP

Ok here is what I have going on.
I am listing the contest of a directory so I can edit and delete files.
The code I am using to do that is below (Other then editing as it does not work).
<?php
$directory = ("enctlfiles/");
$dir = opendir($directory);
$files = array();
while (($file = readdir($dir)) != false) {
$files[] = $file;
}
closedir($dir);
sort($files);
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><TH>File Name</TH><th>Modified Date</th><th>Entries In File</th><th>Delete | Edit</th></TR>\n");
foreach ($files as $file)
{
if (strpos($file, '.jpg',1)||strpos($file, '.ctl',1) ){
echo "<TR><TD><a href=$directory$file>$file</a></td>";
echo "<td>";
echo date("F d Y H:i:s",filemtime($directory. $file));
echo "<td>";
echo count(file($directory. $file));
echo "<td>";
echo "<a href=enctlfiles/dodelete.php?file=$file>Delete</a> | <a href=enctlfiles/editfile.php?file=$file>Edit</a>";
}
}
?>
That lists all of the files in order in a nice pretty table. The delete function works. The edit one not so much . It is due to me not knowing what I am doing I am sure of it.
Here is the contents of the editfile.php
<form action="edit.php" method="post">
<TEXTAREA NAME="save" COLS=150 ROWS=50 wrap="off">
<?php
$dir = ".";
include($dir. '/' .$_GET['file']);
?>
</textarea>
<P><INPUT TYPE="SUBMIT" VALUE="Update File"><INPUT TYPE="RESET">
</FORM>
That populates the textarea with the file contents when you click on the "Edit" link. I don't know if that is the right way to get the data in there to be able to edit the file.
Here is the contents of the edit.php file:
<?php
//I have tried this but it give me that Unable to open file.
//$org = ($_GET['file']) or exit("Unable to open file!");
//This returns me to the prodsh.php page but does not edit the file.
$org = $_POST['save'] or exit("Unable to open file!");
$copy = $org . date("-m-d-Y-His");
copy($org,$copy);
$f = fopen($org, 'w');
fwrite($f, $_POST['save']);
fclose($f);
header('Location: http://somesite.com/GroveTuckey/itemsetup/prodsh.php');
?>
I think the editfile.php and the edit.php file are jacked up but don't know where.
I am trying to learn this and this site has been very helpful when I get stumped on something so I thank you in advance for the help given.
Also I know the danger of editing files via a webpage. This is not public or on a server that is accessible by the world. Right now I don't want to deal with a database.
I have edited files using the below code:
<form action="doedit.php" method="post">
<TEXTAREA NAME="save" COLS=150 ROWS=50 wrap="off">
<?php
include('somefile.ctl');
?>
</textarea>
<P><INPUT TYPE="SUBMIT" VALUE="Update File"><INPUT TYPE="RESET">
</FORM>
With the contents of doedit.php being:
<?php
$org = 'Somefile.ctl';// This is the name of the file in the form above
$copy = $org . date("-m-d-Y-His");
copy($org,$copy);
$f = fopen('somefile.ctl', 'w');//This is the name of said file also.
fwrite($f, $_POST['save']);
fclose($f);
header('Location: http://somesite.com/GroveTickey/editfile.php');
?>
But since the names of the files can be anything I can't put specific names in the script.
This works great other then having to hardcode file names.
You aren't passing the name of the file back to edit.php. Set the form's action to edit.php?file=<?php echo $_GET['file']; ?>.
El codigo para editar lo hice asi:
---Inicio
<? php
$filename = isset($_GET['file']) ? $_GET['file'] : '';
$directory = ("C:\\Linux\\www\\contenido\\"); // Restrict to this directory...
$fn = $directory . $filename;
if (isset($_POST['content']))
{
$content = stripslashes($_POST['content']);
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
<textarea rows="25" cols="100" name="content"><?php readfile($fn); ?></textarea>
<hr />
<input type="submit" value="Guardar">
</form>
back to list
And work great

Categories