saving a picture with a date - php

Good day,
I would like to take screenshots from a ESP32 cam and save it on my NAS. I put the following script on my nas, the picture is saved but the name of the picture is:
always the same which means that the next picture will overwrite the previous one
is a funny name like _OT3HJ~ (and not a date with time as I would have expected...)
Can someone help me a little?
Thanks and best regards,
Yannick
<?php
$url = 'http://192.168.1.232:80/capture';
$img = '.new date("dmYHis").capture.jpg';
file_put_contents($img, file_get_contents($url));
?>

PHP does not allow you to execute functions inside of string definitions like that. Luckily there are are numerous ways to solve this problem.
Simply concatenate the date result and the rest of the file name
$img = date('dmYHis').'capture.jpg';
Use complex syntax to insert the date variable in to the file name string
$date = date('dmYHis');
$img2 = "{$date}capture.jpg";
Use sprintf to build the string
$img3 = sprintf('%dcapture.jpg', date('dmYHis'));

Related

save the last part of url in variable

I want to get the last part of an url that looks like this:
http://localhost:8888/blog/public/index.php/categories/Horror
I've tried it with
$endOfUrl = end(explode('/',$url));
but the thing is I get a notice that "Only variables should be passed by reference"
I need this "Horror" to get it's ID in my database and get all the posts with this id, since I'm trying to code a blog to get experience with php.
Another question linked to this: Is it possible to make it dynamic so it can be used for all the other categories as well? Or do I have to do this for every single category?
I'm new to the world of php so I would really appreciate it if someone could help me on this.
Try like this way for end() but If I were you I will try basename() to get my job done.
<?php
$url = 'http://localhost:8888/blog/public/index.php/categories/Horror';
$exploded = explode('/',$url);
$endOfUrl = end($exploded);
echo $endOfUrl;
?>
Reason why it is not working on single line:
end() requires a reference, because it modifies the internal
representation of the array (i.e. it makes the current element pointer
point to the last element).The result of explode('.', $url) cannot be
turned into a reference and this is a restriction in the PHP language itself.
DEMO: https://3v4l.org/ttKui
Using basename(),
$url = 'http://localhost:8888/blog/public/index.php/categories/Horror';
echo basename($url);
DEMO: https://3v4l.org/pt2cQ

Change entry in a CSV file with PHP

I have a CSV file with three columns:
Mary,150203_15:29:12,150203_16:10:12
John,150203_15:29:17,pending
Peter,150203_15:29:35,150203_15:49:35
Olga,150203_15:30:43,pending
...
..
.
Mary,150204_15:42:14,pending
Peter,150204_20:42:14,pending
Because there are many entries on that file all I want to do is
find the latest entry according to the Username and change the last value (from pending to date()).
In the example above lets say I want to change the latest Mary entry in the 3rd column from pending to date. Then replace the updated CSV file with the current one.
Any ideas on how to approach that?
Thank you
You can work with the file as one huge string and do a string replacement.
$data = file_get_contents('your_file.csv');
$data = str_replace('Joe,150203_16:21:43,pending','Joe,15203_16:21:43,15204_15:23:43',$data);
file_put_contents('your_file.csv', $data);
The comments below raise a concern of finding out what the latest date is for a name. That is also rather simple to do. Assuming you've loaded $data in, as above...
$matches = array(); // Just to point out that this is an array
preg_match_all("/Joe,(.*),/", $data, $matches);
Now, $matches[1] contains all the dates for Joe. Did you ONLY want the ones that are pending? No problem...
preg_match_all("/Joe,(.*),pending/", $data, $matches);
Now, $matches[1] only contains the pending dates. Which is the most recent?
rsort($mathes[1]);
Now, $matches[1][0] is the most recent date. So, you can do:
$data = str_replace('Joe,'.$matches[1][0].',pending','Joe,'.$matches[1][0].',15204_15:23:43',$data);
Is this the absolute most efficient way to do this? No. Is it impossibly hard? No. You should look into using a proper database, but it is possible to use csv files.
If moving the data to a DB is not a solution in your case, you could do the following:
Read the CSV file into PHP via fgetcsv(), line by line, into an
array
Sort the array based on your criteria and get the latest entry
Update the entry
Write the entries back into the file via fputcsv()
I actually found a much simpler solution to this. Was the simplest one I could come up with.
$line = $_SESSION[username].",".$_SESSION[LOGIN_TIME].",".'pending'."\r\n";
$newline = $_SESSION[username].","$_SESSION[LOGIN_TIME].",".date("ymd_H:i:s"). "\r\n";
$data = file_get_contents('login.log');
$data = str_replace($line,$newline,$data);
file_put_contents('login.log', $data);
So there is no need for scanning for the latest entry. I just store the previous entry values and replace them with the new ones.
Thank you kainaw and BigScar

Make SQL column using PHP explode into variable

Hey guys I'm having a bit of trouble trying to make my SQL column a variable in PHP.
So my SQL row contains image URL's and are formatted like this:
http://www.website.com/image.jpg,http://www.website.com/image2.jpg,http://www.website.com/image3.jpg
So now I need to display the first URL in the row.
I have this short line of code:
file = '.(explode(',', $cardata["PictureRefs"])[0]).';
I am basically trying to give the variable a value of:
$file = "http://www.website.com/image.jpg"
My current code is obviously wrong however I feel like I must be quite close. How can I achieve this?
Your short hand is correct, but the string encapsulation is simply not required.
$file = explode(',', $cardata['pictureRefs'])[0]; // first image SRC
Then you can utilize it like:
echo '<img src="'.$file.'"/>';
No problem.
$file=explode(',',$cardata)[0];
Try this:
$file = explode(',',$cardata);
$img = $file[0];

There is an equals sign in the $filename of file_get_contents in my PHP file and it is getting converted to '%3'. How do I deal with this?

I have a php file that takes an image from a website and stores it in my database. The name of the file contains an equals symbol. The file I end up storing is the image I get when I change the equals sign into '%3' in the URL. How can I make it pick up the file that is in the location whose URL maintains the '='?
$idNum=$_GET['idNum'];
$testpage = file_get_contents('http://www.something.com/php/thePic.php?id=$idNum');
$testpage = mysql_real_escape_string($testpage);
mysql_query("UPDATE tblSomething SET Pic = '$testpage' WHERE id='$idNum'");
Thanks,
R
The filename is coming out in a "urlencoded" form.. You need to use the urldecode() function to get back the = sign.
urldecode($string);
See more here - http://php.net/manual/en/function.urldecode.php
So before your SQL query, add this line-
urldecode($field);
And yes.. You are vulnerable to SQL injection attacks..
With these attacks, your WHERE clause can be easily bypassed. Just make sure you dont do any sensitive processing with this kind of a query.

How to extract substrings with PHP

PHP beginner's question.
I need to keep image paths as following in the database for the admin backend.
../../../../assets/images/subfolder/myimage.jpg
However I need image paths as follows for the front-end.
assets/images/subfolder/myimage.jpg
What is the best way to change this by PHP?
I thought about substr(), but I am wondering if there is better ways.
Thanks in advance.
you should save your image path in an application variable and can access from both admin and frontend
If ../../../../ is fixed, then substr will work. If not, try something like this:
newpath=substr(strpos(path, "assets"));
It might seem like an odd choice at first but you could use ltrim. In the following example, all ../'s will be removed from the beginning of $path.
The dots in the second argument have to be escaped because PHP would treat them as a range otherwise.
$path = ltrim('../../../../assets/images/subfolder/myimage.jpg', '\\.\\./');
$path will then be:
assets/images/subfolder/myimage.jpg
I suggest this
$path = "../../../../assets/images/subfolder/myimage.jpg";
$root = "../../../../";
$root_len = strlen($root);
if(substr($path, 0, $root_len) == $root){
echo substr($path, $root_len);
} else {
//not comparable
}
In this way you have a sort of control on which directory to consider as root for your images

Categories