PHP creating folder with two variables gives wrong foldername - php

I'm trying to create a random foldername for upload of files.
The folderpath shall be like "receivedfiles/$name$date/". Instead i get this: "receivedfiles/ . 13.06.2016/". I really don't know what is wrong...
The variable $name is user input and can be e.g. "Simon" or "Simon Jensen" depending on what the user wants.
$d = date('d.m.y');
$varfoldername = "../receivedfiles/. $name . $d ./";
mkdir($varfoldername , 0777 , true);
$upload_folder = $varfoldername;
Thanks to great help i have removed the dots from code above, please see code below which only name the folder with the date.
$name = $_POST['name'];
$d = date('d.m.y');
$varfoldername = "../receivedfiles/$name$d/";
mkdir($varfoldername , 0777 , true);
$upload_folder = $varfoldername;
Solution: $name was first specified later in script. Moving that atop solved part of the problem. Changed folder path as above solved the rest - thank you all :)

. The problem was that this is code from send email form. The email is composed below and fileupload is above. So of course the variable wasn't know at that line. 
Extraordinarily, you've written the solution in the question
$varfoldername = "../receivedfiles/$name$date/";
since it's double-quoted php interprets $.. as variable and print its content
by the way, print $varfoldername to see what is the output

Well, the extra dots are there because you are not concatenating correctly. The 2nd line should be something like
$varfoldername = "../receivedfiles/" . $name . $d . "/";
or you could remove the dots inside your string since you are using double quotes for your folder name value.

First of all, you need to check whether the $name variable is not empty.
Then, use sprintf for creating the path. It provides more flexible formatting and helps you to get rid of redundant spaces/dots/etc. Try this:
$varfoldername = sprintf('../receivedfiles/%s%s/', $name, $d);

Related

If file with name.jpg exists do A else do B

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.

How can I add variable names to the end of a file?

I have created a form that submits the data to a filename on the server. The form submit is working fine, it generates the requested file called "we_input_.sts".
I am trying to use the following code to grab two variables from the form "bfstnme" and "gfstnme"and attach them to the filename eg "wed_import-Jane_Bill.sts
This is the amended code: However I am still unable to get it to work.
I am trying different ideas to get this to work correctly. I have tried moving the code around but I'm still obviously missing something. The last line before the $savestring== is "$fp=fopen("wed-import-.sts", "a+");
The last lines after the $savestring are : fwrite($fp,$savestring); fclose($fp);
<?php
$bfirstname = $_POST['bfstnme'];
$gfirstname = $_POST['gfstnme'];
$file = 'wed_import_.sts';
$current = file_get_contents($file);
$new_file = 'wed_input_'.$bfirstname.'&'.$gfirstname.'.sts';
file_put_contents($new_file, $current);
?>
Here is the way I have solved it using the valuable assistance of all concerned.
$names .= ("$bfstnme" . "$gfstnme");
$fp = fopen("wed_import_($names).sts", "a+");
The results of the above give me a filename called:
"wed_Import_[JaneBill].sts. I only need to work out how to put an amperstand (&) betwen the names.
Thank you to all.
If you want put the info inside the file you must change the + by a . like this:
$current .= ("gfirstname" . "bfirstname");
If you want change the name, you must do something like #Jay_P says
Why you don't name the file before writing to it?
<?php
$gfirstname = $_POST['gname'];
$bfirstname = $_POST['bname'];
$file = 'wed_input_Bride_Groom.sts';
// Opens the file to get existing content hopefully
$current = file_get_contents($file);
// Appends bride and groom first names to the file hopefully
$current .= ("gfirstname" . "bfirstname");
$new_file = 'wed_input_'.$gfirstname.'_'.$bfirstname.'.sts';
// Write the contents back to the file
file_put_contents($new_file, $current);
?>
Let's assume you have the names in a variable called $names. You can easily append the text with the FILE_APPEND flag like this:
file_put_contents('wed_input_Bride_Groom.sts', $names, FILE_APPEND);

php Update filename from directory

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.

Counting sent files

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.

My delete function does not delete the targeted file

Basically I could upload files based on a project. Whenever I create a project, a new directory is created with the directory name as the project_name e.g. this is a test -> this-is-a-test. But my problem is I couldn't delete a file in a directory.
function delete_image($id)
{
$this->load->model(array('work_model', 'project_model'));
$result = $this->work_model->get_work($id);
$result = $this->project_model->get_project($result->project_id);
$dir = str_replace(" ", "-", $result->project_name);
$result = $this->work_model->delete($id);
if (isset($result)){
unlink('./uploads/' . $dir . '/' . $result->full_path);
}
redirect('admin/project/view_project/' . $result->project_id);
}
Need help on this thanks.
Well the error message is self-explanatory.
$result is not an object.
Your problem is matter of debugging, not SO question. And matter of reading error messages, of course. Why do you ask here what's going wrong if you already have explanation from your PHP? And, on the other hand, noone here know your code and have no idea what type of variable get_work($id) supposed to return.
This line is brilliant:
if (isset($result)){
Of course it is set, you have set it 3 times!
Use different variable names for each result you return. Why not use:
function delete_image($id)
{
$this->load->model(array('work_model', 'project_model'));
$work = $this->work_model->get_work($id);
$project = $this->project_model->get_project($work->project_id);
$dir = str_replace(" ", "-", $project->project_name);
if ($this->work_model->delete($id))
{
unlink('./uploads/' . $dir . '/' . $project->full_path);
}
redirect('admin/project/view_project/' . $project->project_id);
}
If that doesn't work, try some debug steps.
var_dump($this->work_model->delete($id));
That will tell you TRUE/FALSE, I'd assume right now it is FALSE which is why unlink isn't erroring or succeeding.
Debug is the way forward. We can't do it for you!

Categories