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!
Related
I am trying to convert my images into greyscale and then downloading it in Laravel but each and every time I am getting this error
The file "" does not exist
Dont' know why it giving this error here is my code.
$file = public_path() . "/large/s/" . $sheet[0]->sheet_f_id . '-s.jpg';
$image = Image::make($file);
$grayScale = $image->greyscale();
return Response::download($grayScale);
When I dump my $file variable I got the response something like this.
"D:\xampp\htdocs\wikistaging\public/large/s/03-02-05-025-s.jpg"
But still it is giving me the sam error why is that happening. Any help would be great.
If you want to download then first you have to save the created file and need to give the path to download. Here is working example
$img_name=$sheet[0]->sheet_f_id . '-s.jpg';
$destination_path=public_path() . "/large/s/";
$file = $destination_path.$img_name;
$image = Image::make($file);
$image->greyscale()->save($destination_path.'gray-'.$img_name);
return Response::download($destination_path.'gray_'.$img_name);
And if you don't want to keep the file you can delete, replace the last line with below line.
return Response::download($destination_path.'gray_'.$img_name)->deleteFileAfterSend(true);
Hope it will work for you.
You must save your image after call greyscale().
You can try it:
$filePath = public_path() . "/large/s/" . $sheet[0]->sheet_f_id . '-s-test.jpg';
$image->greyscale();
$image->save($filePath);
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.
I got a small problem with my PHP webpage. I want to calculate the size of a directory, but I got 2 folders in them, that I don't want to include in the final size. I use following:
function foldersize($directory){
$size = 0;
foreach (glob(rtrim($directory, '/').'/*', GLOB_NOSORT) as $each) {
$size += is_file($each) ? filesize($each) : foldersize($each);
}
return $size;
}
$home_directory = "./files/" . $user_data['unique_id'] . "/";
$dir = foldersize($home_directory);
$dirdel = foldersize($home_directory . "del/");
$dirtmp = foldersize($$home_directory . "tmp/");
$userspace = $dir - $dirdel - $dirtmp;
When I test, which variable the server is able to return I get following result: The server is able to calculate $dir, but it seems to have problems with calculating $dirdel and $dirtmp. So it returns 0. Both folders, however, have files in them. I hope anybody can help me with that. Thank you
i have tried your code and I think is OK - except one small mistake,
$dirtmp = foldersize($$home_directory . "tmp/"); ... there is typo, double dollar, $$home_directory ... other results from function are fine I think
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.
My php script needs to load contents from another file and then replace certain commands. Using the following code works on static pages:
$pageName = 'pages/' . $_REQUEST['url'] . '.php';
$pageContents = file_get_contents($pageName);
$IDCODE = $_SESSION['IDCODE'];
$sql = "SELECT * FROM members WHERE IDCODE = '$IDCODE'";
$qry = mysql_query($sql);
$OK = $qry ? true : false;
$arr = mysql_fetch_array($qry);
foreach ($arr AS $key => $val) {
$pageContents = str_replace('{' . $key . '}', $val, $pageContents);
}
however, what if the file to be processed was dynamic? IE it populates some text from the mysql database.
Will file_get_contents run the file or just read whats in it as a string?
If you run the link to the file via a webserver, it will be processed. If you link it directly, you will get the exact contents of the file.
Rather messy code.
$pageName = 'pages/' . $_REQUEST['url'] . '.php';
$pageContents = file_get_contents($pageName);
local file inclusion vulnerability here.
$OK = $qry ? true : false;
Why? Anywere you use the value of $OK you could equally use $qry. And you never use $OK again in the code shown.
There's no error checking, no comments in the code.
what if the file to be processed was dynamic?
WTF? Do you mean you want to re-process the output as PHP? Then you could 'eval($pageContents);' but beware of code injection vulnerabilities.
Or you want to apply your script to the output of a PHP scrpt? Then just pass the URL as the argument to file_get_contents() e.g.
file_get_contents('http://localhost/path/'
. basename($_REQUEST['url'] . '.php');
But really the invocation should be controlled better than this. Both are messy solutions - a templating solution should be just that. Go have a long hard look at (e.g.) smarty