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
Related
Hello and Thanks for the help!
I have Html and PHP file (2 different file):
html:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<form enctype="multipart/form-data" action="Upload_File.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input name="uploadedfile" id="uploadedfile" type="file" /><br />
<br>
<input type="submit" value="Process File" Onclick="__set(document.getElementById('uploadedfile').value);" style="background-color: hsla(240, 100%, 75%,0.3);font-weight: bold;"/>
</form>
</body>
</html>
Php:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<form enctype="multipart/form-data" action="Upload_File.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input name="uploadedfile" id="uploadedfile" type="file" /><br />
<br>
<input type="submit" value="Upload File" Onclick="__set(document.getElementById('uploadedfile').value);" style="background-color: hsla(240, 100%, 75%,0.3);font-weight: bold;"/>
</form>
</body>
</html>
<?php
$uploadOk = 1;
$target_path_file = "Upload_Files_All/";
$N=$_FILES['uploadedfile']['name'];
$target_path = $target_path_file . basename($N);
$size=$_FILES['uploadedfile']['size'];
$FileTypeExt = pathinfo($target_path,PATHINFO_EXTENSION);
if (isset($N))
{
if (empty($N))
{
echo "<p style='color:#ff0000;' >Please choose a file to Upload</p>";
}
else
{
if ($uploadOk==1)
{
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "<strong>The file ". basename( $_FILES['uploadedfile']['name'])." is ready to upload</strong>";
}
else
{
echo "<p style='color:#ff0000;' >There was an error uploading the file, please Contact Administrator</p>";
}
}
}
}
?>
Now I want to upload files to the system
files name:
sample.xlsx
sample1.xlsx
How I modify the code that only files with the same name (sample.xlsx or sample1.xlsx) will be uploaded.
and file with different name give the user message "please change file name"
There is a not so secure method using HTML5 and input/accept.
You can use it in a Intranet Application where security is not so important.
Such Things should always be checked by the server in production.
DO NOT USE IT ON INTERNET
Only combined with a server check.
Using XLSX Mime application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
AND/OR XLS Mime, application/msexcel
<input name="uploadedfile" id="uploadedfile" type="file" accept="application/msexcel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /><br />
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<form enctype="multipart/form-data" action="Upload_File.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input name="uploadedfile" id="uploadedfile" type="file" accept="application/msexcel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /><br />
<br>
<input type="submit" value="Process File" Onclick="__set(document.getElementById('uploadedfile').value);" style="background-color: hsla(240, 100%, 75%,0.3);font-weight: bold;"/>
</form>
</body>
</html>
THE BETTER WAY
Is using the $_FILES variable
$_FILES['uploadedfile']['type'];
You can check the Mime Type
<?php
$uploadOk = 1;
$target_path_file = "Upload_Files_All/";
$accepted_mime1 = "application/msexcel";
$accepted_mime2 = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
$T=$_FILES['uploadedfile']['type'];
$N=$_FILES['uploadedfile']['name'];
if($T != $accepted_mime1 && $T != $accepted_mime2){
// We do not accept it and we unset the $_FILES
unset($_FILES);
}
$target_path = $target_path_file . basename($N);
$size=$_FILES['uploadedfile']['size'];
$FileTypeExt = pathinfo($target_path,PATHINFO_EXTENSION);
if (isset($N))
{
if (empty($N))
{
echo "<p style='color:#ff0000;' >Please choose a file to Upload</p>";
}
else
{
if ($uploadOk==1)
{
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "<strong>The file ". basename( $_FILES['uploadedfile']['name'])." is ready to upload</strong>";
}
else
{
echo "<p style='color:#ff0000;' >There was an error uploading the file, please Contact Administrator</p>";
}
}
}
}
?>
I'm trying to upload a file in php and unable to do it. The file I'm trying to upload is a csv file, but it should not be a concern. I'm using php to upload my file. I'm also trying to process the form in the same page. Below is my code for file upload and it is not working...
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
if(isset($_POST['csv_file'])) {
echo "<p>".$_POST['csv_file']." => file input successfull</p>";
fileUpload();
}
}
function fileUpload () {
$target_dir = "var/import/";
$file_name = $_FILES['csv_file']['name'];
$file_tmp = $_FILES['csv_file']['tmp_name'];
if (move_uploaded_file($file_tmp, $target_dir.$file_name)) {
echo "<h1>File Upload Success</h1>";
}
else {
echo "<h1>File Upload not successfull</h1>";
}
}
?>
update your form code with enctype attribute
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype = "multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
use enctype="multipart/form-data"
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
I have try below code and it's work perfect.
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])) {
echo "<p>" . $_POST['csv_file'] . " => file input successfull</p>";
$target_dir = "images ";
$file_name = $_FILES['csv_file']['name'];
$file_tmp = $_FILES['csv_file']['tmp_name'];
if (move_uploaded_file($file_tmp, $target_dir . $file_name)) {
echo "<h1>File Upload Success</h1>";
} else {
echo "<h1>File Upload not successfull</h1>";
}
}
?>
</body>
</html>
It should be something like this
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
if ($_FILES['csv_file']['size'] > 0)
{
echo "<p>".$_FILES['csv_file']['name']." => file input successfull</p>";
fileUpload();
}
}
function fileUpload () {
$target_dir = "var/import/";
$file_name = $_FILES['csv_file']['name'];
$file_tmp = $_FILES['csv_file']['tmp_name'];
if (move_uploaded_file($file_tmp, $target_dir.$file_name)) {
echo "<h1>File Upload Success</h1>";
}
else {
echo "<h1>File Upload not successfull</h1>";
}
}
?>
</body>
Below are the changes you need to make
Add enctype = "multipart/form-data" in form tag as new attribute
Change from this if(isset($_POST['csv_file'])) { to this if($_FILES['csv_file']['size'] > 0) { as you need to check size
whether its uploaded or not
Change from this echo "<p>".$_POST['csv_file']." => file input
successfull</p>"; to this echo "<p>".$_FILES['csv_file']['name']."
=> file input successfull</p>"; as you need to use $_FILES to get file name instead of $_POST
Last but not the least, complete </body> tag if you have not yet.
Upload code in PHP[without checking its extension]
<?php
if(isset($_POST['save'])){
$path="var/import/";
$name = $_FILES['csv_file']['name'];//Name of the File
$temp = $_FILES['csv_file']['tmp_name'];
if(move_uploaded_file($temp, $path . $name)){
echo "success";
}else{
echo "failed";
}
}
?>
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="save" value="submit">
</form>
First Give permission to folder where you going to upload Ex: "var/import/" folder.
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype= "multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
if(isset($_FILES['csv_file']) && $_FILES['csv_file']['size'] > 0) {
echo "<p>".$_FILES['csv_file']['name']." => file input successfull</p>";
fileUpload();
}
}
function fileUpload () {
$target_dir = "var/import/";
$file_name = $_FILES['csv_file']['name'];
$file_tmp = $_FILES['csv_file']['tmp_name'];
if (move_uploaded_file($file_tmp, $target_dir.$file_name)) {
echo "<h1>File Upload Success</h1>";
}
else {
echo "<h1>File Upload not successfull</h1>";
}
}
for uploading files, the trick is enctype="multipart/form-data"
use this form definition
I'm stuck with a simple php code, which must move a uploaded file from tmp to a desired location. Please find my code below. I'm not sure where i'm going wrong.
Any help is appreciated.
php code:
<?php
define("UPLOAD_DIR", "/srv/www/wordpress/mywebpage/uploads/");
$scr = $_FILES["jb_cv"]["tmp_name"];
$desttmp = $_FILES["jb_cv"]["name"];
$dest = UPLOAD_DIR .$desttmp;
echo " source file : ";
echo $scr."\t";
echo " destination file : ";
echo $dest;
$success = move_uploaded_file($scr,$dest);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
}
?>
HTML code:
<html>
<head>
<title>Form</title>
</head>
<body>
<h1>Enter your name</h1>
<form method="post" action="move.php" id="contactform" name="contactform" enctype="multipart/form-data">
<label> Upload your CV</label> <input name="jb_cv" type="file" />
<input type="submit" value="SUBMIT" />
</form>
</body>
</html
I just made a file upload code and I was wondering why the file upload wasn't working? I checked the permission of my image folder in localhost and that seems to be ok. I don't know where the problem exactly is. Any ideas?
<?php
require "config.php";
require "functions.php";
require "database.php";
if(isset($_FILES['fupload'])){
$filename = addslashes($_FILES['fupload']['name']);
$source = $_FILES['fupload']['tmp_name'];
$target = $path_to_image_directory . $filename;
$description = addslashes($_POST['description']);
$src = $path_to_image_directory . $filename;
$tn_src = $path_to_thumbs_directory . $filename;
if (strlen($_POST['description'])<4)
$error['description'] = '<p class="alert">Please enter a description for your photo</p>';
if($filename == '' || !preg_match('/[.](jpg)|(gif)|(png)|(jpeg)$/', $filename))
$error['no_file'] = '<p class="alert">Please select an image, dummy! </p>';
if (!isset($error)){
move_uploaded_file($source, $target);
$q = "INSERT into photo(description, src, tn_src)VALUES('$description', '$src','$tn_src')";
$result = $mysqli->query($q) or die (mysqli_error($myqli));
if ($result) {
echo "Succes! Your file has been uploaded";
}
createThumbnail($filename);
}
}
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Upload</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>My photos</h1>
<ul><?php getPhotos(); ?></ul>
<h2>Upload a photo</h2>
<form enctype="multipart/form-data" action="index.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type="file" name="fupload" /><br/>
<textarea name="description" id="description" cols="50" rows="6"></textarea><br/>
<input type="submit" value="Upload photo" name="submit" />
</form>
<?php
if (isset($error["description"])) {
echo $error["description"];
}
if (isset($error["no_file"])) {
echo $error["no_file"];
}
?>
</body>
</html>
Please make sure that you appended directory separator in $path_to_thumbs_directory.
And you don't need to use addslashes to $filename.
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)