Simple PHP echo help - php

How could I add an echo in here
move_uploaded_file($_FILES["torrent"]["tmp_name"], "uploads/ (HERE) .torrent");
I have tried a few things with no luck.

It's just a mere guess, but perhaps you want to do something like that:
$newFileName = 'someFielenameGeneratedByYourScript';
move_uploaded_file($_FILES["torrent"]["tmp_name"], "uploads/" . $newFileName . ".torrent");

You could do something like:
$location = "uploads/" . $name . ".torrent";
move_uploaded_file($_FILES["torrent"]["tmp_name"], $location);
echo $location;

You don't need to echo, since you are passing it to a function not trying to display it in the output.
move_uploaded_file($_FILES["torrent"]["tmp_name"], "uploads/ ".$variable." .torrent");

Related

PHP file_exists does not work. It seems to be reversing true and false

While working on my photo gallery I decided it was best to have an alternate file if image that should display doesn't. I've looked on this site, and may others. They all say to use:
$VariableName = "photography/small/2014-09-21-red-1.png";
if (file_exists ($VariableName)) {echo "Yes!!!";}
else {echo "Nooo!!!";}
or
if (file_exists ("photography/small/2014-09-21-red-1.png")) {echo "Yes!!!";}
else {echo "Nooo!!!";}
For some reason, this will not work for me. It does work, but only when file_exists is set to !file_exists, which is saying: "if this file does not exist, display the image that exists (the image I want, not it's replacement)". In other words:
this is saying if apple exists, display "orange"; and if apple does not exist, display apple.
I've even placed the generated image link in the search bar (when using !file_exists), and after pressing enter, it brings me to the image. I've made sure that the images are set to 0777 in case that's interfering, but it seems to have no effect. All of the $DataRows variables are connected to a database and I've triple-checked that the file names in photography/small match those in the database table.
Why is this happening?
$URLPath = "http://localhost/~matthew/";
if (file_exists ($URLPath . "photography/small/" . $DataRows["DatePublished"] . "-" . $DataRows["FileName"] . "." . $DataRows["ImageExtension"])) {
echo '<img src="' . $URLPath . "photography/small/" . $DataRows["DatePublished"] . "-" . $DataRows["FileName"] . "." . $DataRows["ImageExtension"] . '" alt="' . $DataRows["PhotoName"] . '">' . "\n";
}
else {
echo '<img src="' . $URLPath . 'img/no-image.png" alt="Image Not Here">' . "\n";
}
Thank you very much for the help.
What happens if you change $URLPath from
$URLPath = "http://localhost/~matthew/";
to
$URLPath = $_SERVER['DOCUMENT_ROOT'] . "/~matthew/";
It looks like you are checking for a file but passing a URL into the function. It is probably returning false since the URL is not a valid path on your server. I would suggest using the actual path of the file or if you have to use a URL check out this post: How to check if a file exists from a url
Try using clearstatcache() and double check the file names and paths.
if (file_exists ($VariableName or "path-to-image")) {
This is gibberish. As long as "path-to-image" is not null, the operand will evaluate as (bool) true.
It should be:
if (file_exists ($VariableName)) {
It looks like you should be using
If(is_readable($variable)) {
But looking at your later code you are not using the construct you originally quoted.
file_exists() works as described for me in lots of different contexts.

file_get_contents false when url have spaces (encode everything not working)

So, the problem is in this line
$imageString = file_get_contents($image_url);
with urls that have space character it doesn't work. But if I make
$imageString = file_get_contents(urlencode($image_url));
Nothing works.I keep receiving false in the variable.
the ulr is of the kind:
https://s3-eu-central-1.amazonaws.com/images/12/Screenshot from 2016-04-28 18 15:54:20.png
use this function
function escapefile_url($url){
$parts = parse_url($url);
$path_parts = array_map('rawurldecode', explode('/', $parts['path']));
return
$parts['scheme'] . '://' .
$parts['host'] .
implode('/', array_map('rawurlencode', $path_parts))
;
}
echo escapefile_url("http://example.com/foo/bar bof/some file.jpg") . "\n";
echo escapefile_url("http://example.com/foo/bar+bof/some+file.jpg") . "\n";
echo escapefile_url("http://example.com/foo/bar%20bof/some%20file.jpg") . "\n";
i'v faced the same problem and if you search about it you will see all the people tell you to use urlencode(), but No!! urlencode() wont work in this situation...
i used the #Akram Wahid answer and that work perfectly so i recommend it to use for file_get_contents().
and if you wonder what escapefile_url() does in #Akram Wahid answer here little explain for it:
Simply he take the url apart as array and then he use rawurlencode() to encode all parts that contain special characters without the main domain like (http://example.com).
so what the deference?!! here example uses urlencode() and escapefile_url() to clarify this
echo escapefile_url("http://example.com/foo/bar bof/some file.jpg") . "<br>";
// http://example.com/foo/bar%20bof/some%20file.jpg
echo urlencode("http://example.com/foo/bar bof/some file.jpg") . "<br>";
// http%3A%2F%2Fexample.com%2Ffoo%2Fbar+bof%2Fsome+file.jpg
If you want to apply #Akram Wahid's solution to URLs that may also contain GET arguments then an updated version would be this:
function escapefile_url($url){
$parts = parse_url($url);
$path_parts = array_map('rawurldecode', explode('/', $parts['path']));
return
$parts['scheme'] . '://' .
$parts['host'] .
implode('/', array_map('rawurlencode', $path_parts)) .
(isset($parts['query']) ? '?'.rawurldecode($parts['query']) : '')
;
}

Link with 2 variables in 1 variable

I want to find out what the filessize of this file is filesize().
With an echo it works:
$Data[textfile]; = Complete Filename with ending in Database
echo 'http://myurl.com/files/'.$Data[id].'/'.$Data[textFile].'';
But it doesn´t work like this:
$file = "http://myurl.com/files/" . $Data[id] . "/" . $Data[textFile] . "";
The link looks/should look like this:
http://myurl.com/files/123/textfile.docx
edit: sorry for my weird english...i hope the point is clear
Edit 2: I want to get the filesize of a pdf or docx
The rest of the code:
$size = filesize($file);
$size = $size/1048576;
$size = round($size,2);
echo $size." Mb";
try $file = 'http://myurl.com/files/' . $Data['id'] . '/' . $Data['textFile'];
Try:
$file = 'http://myurl.com/files/' . $Data[id] . '/' . $Data[textFile];
try $file = 'http://myurl.com/files/'.$Data[id].'/'.$Data[textFile];

How to ask PHP to find filename that contains something

im trying to do this :
$filename = "/destination/destination2/file_*";
" * " = anything
destination2 files :
file_somethingrandom
and since it $filename contains * so it should be selecting file_somethingrandom
how to do it?
Use the glob() function like this:
$filename = "/destination/destination2/file_*";
foreach (glob($filename) as $filefound)
{
echo "$filefound size " . filesize($filefound) . "\n";
}
Try this
foreach (glob("/destination/destination2/file_*") as $filename) {
echo $filename;
}
Cheers!
You can use different functions other than echo, but it does what it does, randomly picks a filename in a group of files inside "/destination/destination2/" whose name contains "file_".
<?php
$filea = glob('/destination/destination2/file_*');
echo $filea[rand(0,count($filea)-1];
?>

php file put contents reverse

Ok sorry if its a stupid question im a begginer.
Im making a small shoutbox just for practise.
It inserts the shout infos in a txt file.
My problem is that, it lists the text from top to bottom, and i would like to do this reversed.
if(isset($_POST['submit'])) {
$text = $_POST['text'];
if(!empty($text)) {
$text = $_POST['text'];
$name = $_POST['name'];
$time = date("H:i");
$content =
"<div class='text'><em>" . $time . "</em>
<span class='c11'><b>" . "<a href='userinfo_php_willbe_here.php' target='_blank'>" . htmlspecialchars($name) . "</a>" . ":</span></b>
" . htmlspecialchars($text) . "
</div>\n";
file_put_contents($file, $content, FILE_APPEND | LOCK_EX);
}
}
here is my code.
i was googleing around with not much luck maybe i wasnt looking hard enough.
could please someone give me a hint?
thank you
No way to do so with one function call. You need to read the content from your target file, prepend the data in php and rewrite the whole file (see file_get_contents).
$fileContent = file_get_contents($file);
$fileContent = $content . $fileContent;
file_put_contents($file, $fileContent, LOCK_EX);
You can also use the array_reverse like so:
// Data in file separated by new-line
$data = explode("\n",file_get_contents("filename.txt"));
foreach(array_reverse($data) as $value) {
echo $value."\n";
}
You can only prepend to a file by means of reading it and writing afterwards.
file_put_contents($file, $content . file_get_contents($file), LOCK_EX);

Categories