Can't get filesize() from 20 Mb file - php

I'm setting an application that reads information from a ts video file. If the file is 10 Kb, he function works perfectly, but when the file is much longer (around 20.478 KB) it gets frozen, and I can't get any information. No filesize(), neither files() or anything. I tried almost everything but I'm out of ideas.
HTML code:
<!DOCTYPE html>
<html>
<head>
<title>Proyecto</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="leerBytes.php" enctype="multipart/form-data">
Choose a file: <br>
<input type="file" name="file" id="file"> <br>
<input type="submit" name="button" id="button" value="Send" onclick="post"> <br>
</form>
</body>
</html>
PHP code: (leerBytes.php)
<?php
if(isset($_POST["button"])){
echo "File sent <br>";
$file = $_FILES['file'];
echo "<br>";
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode ('.',$fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('ts');
if (in_array($fileActualExt, $allowed)) {
if($fileError === 0){
$fileNameNew = uniqid('',true).".".$fileActualExt;
$fileDestination = 'uploads/' .$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
$byte = new leerBytes();
$byte ->leerByte($fileDestination);
}else{
echo 'Error.';
}
}else{
echo 'Wrong file.';
}
}
class leerBytes{
function leerByte($file){
$filesize = filesize($file);
echo $filesize;
$fileopen = fopen($file, "rb") or die("Can't get fileopen");
$bytesize = 8;
if($fileopen){
$header = null;
$body = null;
echo "There's data";
$buffer = fgets($fileopen, 4096);
echo $buffer;
fclose($fileopen);
} else {
echo "Error";
}
}
}
?>
I expect to get the filesize, or the information from the file in an array.
I leave here some files as example: https://drive.google.com/drive/folders/1Hge4w8cIZBRm3TskABWcQZSoFFVUMx0-?usp=sharing

Related

Upload file code don't upload image in PHP

I was trying to upload a folder inside my htdocs folder in XAMPP.
I followed the rules of move_uploaded_file still did not work.
Here's my current code:
<?php
if(isset($_POST['submit'])){
$allowed_ext = array('png', 'jpg', 'jpeg', 'gif');
if(!empty($_FILES['upload']['name'])){
print_r($_FILES);
$file_name = $_FILES['upload']['name'];
$file_size = $_FILES['upload']['size'];
$file_tmp = $_FILES['upload']['tmp_name'];
$target_dir = "uploads/{$file_name}";
// Get file ext
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
// Validate file ext
if(in_array($file_ext, $allowed_ext)) {
// verify size
if($file_size <= 1000000) { // 1000000 bytes = 1MB
// Upload file
move_uploaded_file($file_tmp, $target_dir);
$message = '<p style="color: green;">File uploaded!</p>';
} else {
$message = '<p style="color: red;">File to large</p>';
}
} else {
$message = '<p style="color: red;">Invalid file type</p>';
}
} else {
$message = '<p style="color: red;">Please choose a file</p>';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload</title>
</head>
<body>
<?php echo $message ?? null; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="upload" />
<input type="submit" value="submit" name="submit" />
</form>
</body>
</html>
Right now the images are not getting move to the uploads folder inside my current directory.
Any idea why?
Try this:
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 1000000){
$errors[]='File size must be excately 1 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"uploads/".$file_name); //create a folder 'uploads' in your folder
echo "Success";
}else{
print_r($errors);
}
}
?>

How to upload files and name them with file type?

hey guys so I created a website that you can upload books to and display them as a list I have a form to input the name of the book and the file type and I want them to display as the name and file type (on the same line) example (nameofabook epub) but when I try is display them it shows up like (nameofabook
new line epub) here's my code thank you
<?php
if (isset($_FILES['file']) && isset($_POST['name'])) {
$file = $_FILES['file'];
$file_name = $file['name'];
$file_type = $file['type'];
$file_tmp = $file['tmp_name'];
$file_size = $file['size'];
$file_error = $file['error'];
$file_ext = explode(".", $file_name);
$file_ext = strtolower(end($file_ext));
$allowed = array("mov", "avi", "mp4", "epub", "pdf"); //The extensions you allow
if (in_array($file_ext, $allowed)) {
if ($file_error === 0) {
if ($file_size <= 2097152) { //the maximum filesize
$file_destination = ''.$file_name; // If ' ', the file will be placed in this directory
if (move_uploaded_file($file_tmp, $file_destination)) {
echo $file_destination;
$fp = fopen('book_list.txt', "a");
fwrite($fp, $_POST['name']. "|||" .$file_destination."\n");
fwrite($fp, $_POST['type']. "|||" .$file_destination."\n");
fclose($fp);
} else {
echo "An error has been encountered while moving your file!";
}
} else {
echo "Your file is too big!";
}
} else {
echo "An error has been encountered while uploading your file!";
}
} else {
echo "You can't upload files of this type!";
}
}
?>
if anyones curious heres my html for the upload page
<!DOCTYPE html>
<html>
<head>
<title>Upload a Book</title>
<link href = "style2.css" type = "text/css" rel = "stylesheet" />
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Name: <input type="text" name="name" required/>
Type: <input type="text" name="type" required/>
File: <input type="file" name="file" required/>
<input type="submit" value="Upload" />
</form>
</body>
</html>
Firstly...
Don't upload files directly in your site! You should upload them with an API, outside, in a cloud storage. In this case, you can use Cloudinary . So, you don't need to think much about the files uploaded. Again, you can show them easily!
Don't store information like names and others(without the file) in a text file. Storing them in a MySQL database is the best way to do so.
Secondly...
Use different inputs for collecting book names and other information so that, you can display and modify them easily.
Thirdly...
Please give the code you are using for displaying data
I'm a Homo sapiens So I can write anything wrong. Please forgive me for that.

i am not able to see uploaded image from simple html and php code

i am making a simple image upload event or function from simple php and html code,although i dont get any errors, i cant see my uploaded image in my folder named 'uploads',what should i do?
....html
<!DOCTYPE html>
<html>
<head>
<title>prasad</title>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit" name="submit">upload</button>
</form>
</body>
</html>
...html
...php
<?php
if (isset($_POST['submit'])){
$file = $_FILES['file'];
print_r($file);
echo "<br>";
$fileName = $_FILES['file']['name'];
$fileType=$_FILES['file']['type'];
$file_temp_name=$_FILES['file']['tmp_name'];
$file_error=$_FILES['file']['error'];
$file_size=$_FILES['file']['size'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg','jpeg','png','pdf');
if (in_array($fileActualExt, $allowed)) {
if ($file_error === 0) {
if ($file_size<1000000){
$fileNameNew = uniqid('',true).".".$fileActualExt;
$fileDestination = 'uplaods/'.$fileNameNew;
move_uploaded_file($file_temp_name, $fileDestination);
header("Location: pp.php?success");
}else {
echo "this is too big file";
}
# code...
}else{
echo "there was an error ..!";
}
}else{
echo "<h1>you couldnt choose any file..</h1>";
}
}
the only error is , i aint able to see my uploaded image

PHP - File-Uploader

I want to create a file-uploader with php.
Does anybody know where the problem is. I am writing this code for the 3rd time, but it does not work...
Can anybody help me???
Thank you!!!
HTML-File:
<html>
<head>
<meta charset="utf-8" />
<title>File Uploader</title>
</head>
<body>
<form action="upload_files.php" method="post" enctype="multipart/formdata">
<input type="file" name="file" />
<input type="submit" value="upload" />
</form>
</body>
</html>
PHP-File:
<?php
if (isset($_FILES['file'])) {
$file = $_FILES['file'];
$file_name = $file['name'];
$file_tmp = $file['file_tmp'];
$file_size = $file['size'];
$file_error = $file['error'];
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
$allowed = array('txt', 'jpg');
if (in_array($file_ext, $allowed)) {
if ($file_error === 0) {
$file_new_name = uniqid('', true) . '.' . $file_ext;
$file_dir = 'uploads/' . $file_new_name;
if (move_uploaded_file($file_tmp, $file_dir)) {
echo $file_dir;
}
}
}
}
?>
You have done mistake here.. enctype="multipart/formdata"
Change this to enctype="multipart/form-data"
<form action="upload_files.php" method="post" enctype="multipart/form-data">
</form>
Check This File Upload

PHP upload file to database not working with gif, jpeg and other filetypes

I have a problem with some PHP/MySQL not working. I can upload files like .docx and .sql perfectly but when i try with .gif, .jpg .zip and .rar, it doesn't work.
Here is the upload form code:
<?php
if (isset($_GET['uploadError']))
{
echo '<div id="error">There was an error uploading the file. Please try again</div>';
}
elseif ((!isset($_SESSION['dbusername']))&&(!isset($_SESSION['dbpassword'])))
{
header('Location: ?page=login&uploadAttempt=true&attemptedSite=upload');
}
else
{
echo '
<center>
<form class="form" enctype="multipart/form-data" action="uploadaction.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30720" />
<p class="uploadfile">
<input name="uploadedfile" type="file" />
</p>
<p class="submit">
<input type="hidden" name="upload" value="start" />
<input type="submit" value="Upload File" />
</p>
</form></center>';
}
?>
Here is the uploadaction.php code:
<?php
require('lib.php');
localhost_con('filehunt');
session_start();
if(isset($_POST['upload']) && $_FILES['uploadedfile']['size'] > 0)
{
$fileName = $_FILES['uploadedfile']['name'];
$tmpName = $_FILES['uploadedfile']['tmp_name'];
$fileSize = $_FILES['uploadedfile']['size'];
$fileType = $_FILES['uploadedfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
/*$fh = fopen($_FILES['uploadedfile']['tmp_name'], 'r');
$theData = fread($fh, filesize($_FILES['uploadedfile']['tmp_name']));
$theData = mysql_real_escape_string($theData);
fclose($fh); */
$date = date("y/m/d : H:i:s", time());
$sql = "INSERT INTO files (rowID, file, mimetype, data, uploaded_by, uploaded_date, size, times_downloaded)
VALUES (NULL, '$fileName', '$fileType', '$content', '$user', '$date', $fileSize, 0);";
if (mysql_query($sql,$con))
{
header('Location: index.php?page=search&uploadSucces=true');
}
else echo mysql_error();
}
else header('Location: index.php?page=upload&uploadError=true');
?>
It gets inserted in the database, but the mimetype and data column is empty.
Can anyone tell me why this is happening, and how to fix it?
Thank you in advance
Adam
I had the same problem with other type of files. The workaround is encoding the file with base64 that is safer to transmit (do not contain invalid characters that may truncate the streaming).
Not quite shure, but this:
$fileType = $_FILES['uploadedfile']['type'];
must be like this:
$finfo = new finfo();
$fileType = $finfo->file($_FILES['uploadedfile']['tmp_name'], FILEINFO_MIME);

Categories