PHP: Problem with filesize - php

I need a little help here:
I get a file from an HTML upload form. And I have a "target" filename in $File.
When I do this:
copy($_FILES['binfile']['tmp_name'], $File);
echo '<hr>' . filesize($_FILES['binfile']['tmp_name']);
echo '<hr>' . filesize($File);
Everything works fine. I get the same number twice.
However when I delete the first call of filesize(), I get "0" (zero).
copy($_FILES['binfile']['tmp_name'], $File);
echo '<hr>' . filesize($File);
Any suggestions? What am I doing wrong? Why do I need to get the filesize of the "original" file before I can get the size of the copy?
(That's actually what it is: I need to call the filesize() for the original file. Neither sleep() nor calling filesize() of another file helps.)
System:
Apache 2.0
PHP 5.2.6
Debian Linux (Lenny)

How big is this file? You are doing a copy and then stating the file straight away. Could this be the problem?
Does the builtin move_uploaded_file() function give the same behavior?

Try this:
copy($_FILES['binfile']['tmp_name'], $File);
clearstatcache();
$filesize = $_FILES['binfile']['size'];
echo '<hr>' . $filesize;

How about this:
copy($_FILES['binfile']['tmp_name'], $File);
clearstatcache();
while (empty(filesize($File)))
sleep(2);
echo '<hr>' . filesize($File);
OR try this:
copy($_FILES['binfile']['tmp_name'], $File);
clearstatcache();
while (!file_exists($File))
sleep(2);
echo '<hr>' . filesize($File);

Related

How to get PDF or DOC file size?

Trying to get a pdf document file size, but it is giving me Warning: filesize(): stat failed for...
What I have tried:
//get original temporary name
$temp_name = $_FILES['userDegreeFile']['tmp_name'];
//original file path with original name
$inital_name = $_SERVER['DOCUMENT_ROOT'] . "super/IMG/user_files/user_files_".$userFirstName."_".$user_id."/" . $temp_name;
//get the file size
$file_size = filesize($inital_name);
When running this I get the error stated above. The file name in the error is D:/Programs/wamp/www/super/IMG/user_files/user_files_Jim_52/D:\Programs\wamp\tmp\php70B9.tmp
What am I doing wrong?
Always use DIRECTORY_SEPARATOR, its compatible for both linux and windows path.
$inital_name = $_SERVER['DOCUMENT_ROOT'] . "super".DIRECTORY_SEPARATOR."IMG".DIRECTORY_SEPARATOR."user_files".DIRECTORY_SEPARATOR."user_files_".$userFirstName."_".$user_id.DIRECTORY_SEPARATOR."$temp_name;
Another way is $_FILES ["file"]["size"]; Check here for more info : How to use $_FILES["file"]["size"]?
Please try this I think this will help you
`
// outputs e.g. somefile.txt: 1024 bytes
$filename = 'somefile.txt';
echo $filename . ': ' . filesize($filenam`) . ' bytes';
?>
`

Downloading/Populating CSV File with SendFile

I am using the PHP Yii framework and trying to download my file. Currently, it downloads, but the file is populated with the $filePath location. Why doesn't this populate my file correctly?
$getData = // Getting data here.
$fileName = "file.csv";
$filePath = __Dir__ . "/.../exports";
$file = fopen($filePath . '' . $fileName, "w");
fputcsv($file, array_keys($getData[0]));
foreach ($getData as $row){
fputcsv($file, $row);
}
header("Content-Type: text/plain");
header("Content-disposition: attachment; filename = $fileName");
header("Pragma: no-cache");
Yii::app()-> request -> sendFile($fileName, $filePath);
Two things. One, i think you are missing a / between $filePath and $fileName variables. Shouldn't it be something like:
$file = fopen($filePath . '/' . $fileName, "w");
Second, I think the way you are calling sendFile() is incorrect. The second parameter is not expected to be the path to the file. Rather, it is expected to be the actual content that should be sent to the browser. So try changing that line to something like:
Yii::app()->request->sendFile($fileName, file_get_contents($filePath . '/' . $fileName));
Here, in the second parameter we are providing the contents of the file in the path specified by $filePath . '/' . $fileName.
After further research, I was able to solve my error. I needed to add file_get_contents instead of its actual path.
Original:
Yii::app()-> request -> sendFile($fileName, $filePath);
Modified:
Yii::app()-> request -> sendFile($fileName, file_get_contents($filePath . "/" . $fileName));

Problem to upload audio file in php

Here is my code:
if ($_FILES['music']['name'] != '')
{
$file_name = time() . $_FILES['music']['name'];
copy($_FILES['music']['tmp_name'], "music/" . $file_name);
}
else
{
$file_name = "";
}
I want to upload audio file. the file name is insert into database. but its not insert into folder.
try move_uploaded_file instead. You may want to check file size limits too.
I think you want to use move_uploaded_file(), not copy
I guess the folder in which you are trying to copy the uploaded file is not the one you expect. Possibly this is what you need:
copy($_FILES['music']['tmp_name'], __DIR__ . "/music/" . $file_name);
By the way move_uploaded_file() works faster and safer than copy().

Keeping some files private

I was trying to create a directory of private files that could only be accessed when a user logs in. To do this, I used a folder outside the web directory, and then php to access it, if allowed.
Here's an example:
function display_movie($file){
printf("`<video id='movie' width='960' height='416' controls='controls' onerror='fix()'>`<br/>
`<source src='movie.php?file=%s' type='video/ogg; codecs=\"theora, vorbis\"'>
</video>", rawurlencode($file)`);
}
This works great for images, but breaks the media player. Also, I've only tested this locally on a Linux machine.
Any ideas? Thanks.
This is in movie.php...
if(file_exists($fileDir . md5($file) . $ext)) {
$contents = file_get_contents($fileDir . md5($file) . $ext);
}
header('Content-type: video/ogg');
echo $contents;
What is with the <br /> in you're movie.php ?
Did you tryed to see if movie.php echoes the file contents ? ( view source on you're page , then copy the movie source src "movie.php?file=..." and paste it in you're browser , see if it get's the movie out ) .
Allso you need to move the header and echo inside the if statement , if that file doens't exist you can echo a different standard movie :
if(file_exists($fileDir . md5($file) . $ext)) {
$contents = file_get_contents($fileDir . md5($file) . $ext);
} else {
$contents = file_get_contents($fileDir . md5(MOVIE_NOT_FOUND));
}
header('Content-type: video/ogg');
echo $contents;
Where MOVIE_NOT_FOUND is a constant, movie you whant to display if the requested one is not found .
One other thing you could do is enter you're full uri to the movie php in you're source src ( like "http://localhost/some/uri/movie.php?file=..."
From where do you get $fileDir , $file and $ext in you're movie.php ?
Edit: should take care of the file_get_contents problem you have
$file = $fileDir . md5($file) . $ext;
header('Content-type: video/ogg');
//set aditional headers you may whant here
ob_clean();
flush();
if( file_exists($file) )
{
readfile($file);
} else {
readfile($fileDir . md5(MOVIE_NOT_FOUND));
}
exit;

PHP change file extension

I am trying to change a file exenstion, but whenever I do the file seems to corrupt.
$oldFileName = $targetDir . DIRECTORY_SEPARATOR . $fileName;
$newString = preg_replace('"\.tmp$"', '.jpg', $oldFileName);
rename($oldFileName, $newString);
The code works and changes the extension, but yet the file when downloaded, comes up as being corrupt.
The exension is .tmp and I am trying to change it to .jpg.
If I download the .tmp and manually change it to a .jpg it works, but not when the PHP does it.
Anyone know why this may be happening?
Thanks!
try this
<?php
$file = 'example.txt';
$newfile = 'example.txt.bak'; //new file with extension
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>

Categories