The following code appears to upload ZIPs, but does not in fact create the document according to the naming convention I'm using:
// Upload ZIP
if((isset($_FILES['arquivo']['name'][0])) && ($_FILES['arquivo']['type'] == 'application/zip, application/octet-stream') && ($_FILES['arquivo']['size'] < 1000000)){
$arq = 'documento_'.uniqid().".zip';
$path = '/home/domain_name_here/www/documentos/';
$documento = $path.$arq;
move_uploaded_file($_FILES['arquivo']['tmp_name'], $documento);
chmod($documento, 0777);
}
Not entirely sure why this isn't functioning the way it should. Seems like the files upload to temporary and fail somewhere between there and the documentos folder.
Thanks in advance!
I would suspect that this line contains an error:
$arq = 'documento_'.uniqid().".zip';
You have double quote instead single quote wrapping .zip. It should be:
$arq = 'documento_'.uniqid().'.zip';
Related
I need to develop a little PHP script that I can run from a cron job which in pseudo code does the following:
//THIS IS PSEUDO CODE
If(file exists with name 'day.jpg')
rename it to 'fixtures.jpg'
else
copy 'master.jpg' to 'fixtures.jpg'
Where day.jpg should be the current day of the month.
I started to replace the pseudo code with the stuff I'm pretty sure how to do:
<?php
if(FILE EXISTS WITH NAME DAY.JPG) {
rename ("DAY.JPG", "fixtures.jpg");
} else {
copy ("master.jpg", "fixtures.jpg");
}
?>
Clearly there are still a few things missing. Like I need to get the filename with the current day of the month and I need to check if the file exists or not.
I guess I need to do something like this $filename='date('j');'.jpg to get the filename, but it isn't really working so I kinda need a bit help there. Also I don't really know how to check if a file exists or not?
$path = __DIR__; // define path here
$fileName = sprintf("%s%d.jpg", $path, date("j"));
$fixtures = $path . DIRECTORY_SEPARATOR . "fixtures.jpg";
$master = $path . DIRECTORY_SEPARATOR . "master.jpg";
file_exists($fileName) ? rename($fileName, $fixtures) : copy($master, $fixtures);
Basicly you need script like above but you need to work on your path. Your code above had syntax problem.
You have a basic syntax problem, it should be:
$filename = date('j') . '.jpg';
You don't put function calls inside quotes, you need quotes around the literal string '.jpg', and you need to use . to concatenate them.
I recommend you read the chapter on Strings in a PHP tutorial.
so the title is not full clear, my question , I'm using the code to rename the file from directory present in the server the problem is i have to use the HTML form and php to update the file name, i want to do this : there will be an option on every file for renaming it when i click on the option the box pops up and i have to type the new name for file and save it , any help will be appreciated. (before down voting think about the question.)
The code that I'm using to update the file name
<?php
include("configuration.php");
$target = $_POST['filename'];
$newName = $_POST['newfilename'];
$actfoler = $_REQUEST['folder'];
$file = "files/users/";
$new ="files/users/";
$renameResult = rename($file, $new);
// Evaluate the value returned from the function if needed
if ($renameResult == true) {
echo $file . " is now named " . $new;
} else {
echo "Could not rename that file";
}
header("Location:".$_SERVER["HTTP_REFERER"]);
?>
Try changing these lines:
$file = "uploads/$loggedInUser->username$actfolder/$target";
$new ="uploads/$loggedInUser->username$actfolder/$newName";
To:
$file = "uploads/{$loggedInUser->username}{$actfolder}/{$target}";
$new ="uploads/{$loggedInUser->username}{$actfolder}/{$newName}";
To explain why:
You are using variables inside a string, which means you will want to tell PHP where the variable ends. Especially when referencing objects or arrays, but also when you are placing variables right next to each other. I'm guessing PHP evaluated your original line to uploads/[Object]->usernamePizza/newname
I don't think you can call object properties in a string as you do.
try replace these lines :
$file = "uploads/".$loggedInUser->username."$actfolder/$target";
$new ="uploads/".$loggedInUser->username."$actfolder/$newName";
You may think about echoing $file and $new to confirm the path is nicely built.
On a side note, I'd recommend to really check the entries because this code can obviously lead to major security issues.
I want make a loop of my fold, get all the files and make a judge, print all the files name witch size are less than 10kb. But I get nothing from this code (no php error hint, just 0 result, and I am sure there has 10 files at lest < 10kb), where is the problem? Thanks.
$folder = dirname('__FILE__')."/../images/*";
foreach(glob($folder) as files){
$size = filesize(files);
if($size<10240){
echo files.'<br />';
}
}
I think there's a typo, because
dirname('__FILE__')
should be (without quotes)
dirname(__FILE__)
and also, your variable files doesn't have a dollar sign
$size = filesize($files);
and also here echo $files
That's it, it should fix your problem
__FILE__ is a magic constant, therefore you cannot wrap it in quotes:
$folder = dirname(__FILE__)."/../images/*";
You missed a $ in files:
$size = filesize($files);
// and
echo $files.'<br />';
Are you sure
$folder = dirname('__FILE__')."/../images/*";
is valid? do you mean
dirname(__FILE__)
I have an multiple input sending files and I need guard this images with another name inside my folder called 'home';
So the pictures filing with the name home1.jpg, home2.jpg, etc
So, here is my code:
$file = $_FILES['Filedata'];
$filename_home = "";
$img_array = array($filename);
foreach($img_array as $key=>$value){
$filename_home.="home".$key.".jpg";
}
But this doesn't producing the result.
Any help, will be appreciate
Where does $filename come from? It looks like you want to use $file instead.
Has anyone got any idea to why doesn't the following work ?
$file = 'images/thumbs/1%20-%20Copy.jpg';
if(!file_exists($file)){
die('NOT THERE');
}
echo 'Yes its there.';
The problem is with the spaces. I have checked the file exists,dbl checked n triple checked im going nuts. :(
Help
file_exists works on the file system and not via HTTP. So %20 will not be recognized as space but literally as %20; use spaces instead:
$file = 'images/thumbs/1 - Copy.jpg';
$file = rawurldecode('images/thumbs/1%20-%20Copy.jpg');
try these two
$file = 'images/thumbs/1\ -\ Copy.jpg';
$file = 'images/thumbs/1 - Copy.jpg';