How to load an image from PHP to a flash movieclip? - php

First of all, let me say that I have no actionscript knowledge, but I have PHP knowledge.
How can I make a movieclip display an image from a php file?
And how can I send the image from the php file to the movieclip?
Do I echo it?

Alright, i know you could have just used the googles to look this up, but I'm gonna explain how to do it anyway.
How can I make a movieclip display an image from a php file?
<?php
// open the file in a binary mode
$name = './img/ok.png';
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
// dump the picture and stop the script
fpassthru($fp);
exit;
This code was copy-pasta'd from the php.net manual. In short you should use fpassthru
How can I send the image from the php file to the movieclip?
Use the Loader class. An example is at the bottom of the page.

Related

PHP file that accepts file path from loop and returns an image

I am trying to display multiple images from my database through a loop. Basically it's something like this
while loop is running{
$_SESSION['path'] = $imageURL;
echo '<img src="pic.php">';
}
my idea is the pic php gets the path then displays it. Then another path comes in and it returns the image again.
HOWEVER IT DID NOT. It just returns the last image retrieved and repeat it until the loop is done.
so instead of displaying a.png, b.png and c.png. What displayed were 3 c.png.
here is my pic.php
<?php
session_start();
$name = $_SESSION['path'];
$fp = fopen($name, 'rb');
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
fpassthru($fp);
exit;
?>
I'm particularly new to this stuff so it will be a great help if you guys check this out! TYIA!
Here is what happens:
You access your web page through your favorite browser
The server process your request, and asks PHP to create an HTML file containing 3 <img src="pic.php" />
The server returns the HTML file to your browser
The browser analyze your HTML file and detect it has links to external resources, indeed it has 3 times pic.php, so it begins to ask the server to return the content of this file
The server process again the request and asks PHP to return to him the content of pic.php
The server returns this content, and HTML put it 3 times
You can see at the end, even if you looped 3 times and changed 3 times $_SESSION['path'], the server comes after the war and only see c.php, so it returns it to the browser.
You should adopt another strategy to fetch your images.
Workaround
One way to fix the issue is to fetch image "at demand" like this:
pic.php
<?php
session_start();
$name = filter_var($_GET['q'], FILTER_SANITIZE_URL);
$fp = fopen($name, 'rb');
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
fpassthru($fp);
exit;
?>
index.php (the file you access)
<?php
while loop is running{
echo "<img src='pic.php?q=$imageURL'>";
}
?>

Php header() User Agent Change

$file_name = $_GET['title'];
$file_url = $_GET['url'] . $file_name;
header('Content-Type: video/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
exit;
I'm using this code to download files in my site fetching from another websites.
It works if my url looks like:-
https://www.example.com/video_download.php?title=video.mp4&url=http://googlevideo.com/video/download/223689/289048
(example)
So, it starts downloading by fetching the video file from http://www.googlevideo.com/video/play/221589 to my site.
But my problem is that the file can be accessed if the person uses a PC.
Can I change the User Agent by using header()?
Is it possible?
So if I change the user agent into a PC user agent, so it can be downloaded from a mobile!
I'm sorry, but the User Agent has nothing to do with readfile() function. Readfile() will just throw the raw file input into your browser. Useful for e.g. rendering images through PHP to the client without having to expose the real file name.
Indeed, it is possible to render video to the client with readfile(), but using a HTML5 video tag will dramatically improve performance. This will also provide better mobile support.
Hope this helps you,
You can use stream_compy_to_stream
$video = fopen($file_url);
$file = fopen('videos/' . $title . '.mp4', 'w');
stream_copy_to_stream($video, $file); //copy it to the file
fclose($video);
fclose($file);
I wrote a class for downloading youtube video files. you can find it here.

Unable to display PDF using php - just says LOADING

Can anyone tell me why I am unable to display a PDF file using PHP? It says LOADING in lower left corner which never goes away. I can use the control panel and view the pdf just fine, so I know it's a valid PDF file.
Here's the code:
<?php
session_start();
$path = '/show_bills/';
// The location of the PDF file on the server.
$filename = $path.$_SESSION['ShowID']."_show_bill.pdf";
header("Content-type: application/pdf");
header("Content-Length: " . filesize($filename));
readfile($filename);
exit;
?>
Thanks,
Vic
I am almost sure the file just does not exist. You have a trailing slash in the $path meaning the the script will look for the file in the very root of the server
You can check whether the file exists or not using file_exists function
http://uk3.php.net/manual/en/function.file-exists.php
Also, just try to output the file without specifying headers - probably it outputs a PHP warning

readfile equivalent for binary data?

I'm using http://undesigned.org.za/2007/10/22/amazon-s3-php-class/documentation to access private files using php. I can get the data of the file by saying $object->body. I actually want to see the image in the browser or play the video in a video player. Is there a way to do that?
I think I need something like readfile. The problem is readfile is I need the path to the file. The path is private so I cannot use that. Is there a way to do a readfile of the binary data?
I put this in the php thinking this would help but it still displays the binary data.
header('Content: image/jpeg');
header('Content-Disposition: inline; filename=IMAG0108.jpg');
echo $object->body;
You just set the content-type header and output the readfile to the browser. What I do is create a new php file, like "showimage.php", that accepts an ID or some such to know what image to display. Then I use it in a browser page: .
In showimage.php, something like:
<?php
header('Content-type: image/png');
readfile('/var/images/' . $_GET['id'] . '.png');
// or
// echo $object->body;
?>
That would read a file from the local system and output it as an image. Off the top of my head, so I might have messed up that code!
header('Content: image/jpeg');
echo $object->body;
Should work fine (for JPEGs), you need know what filetype is in question and then send appropriate content headers.

send a file to client

I want to write a text file in the server through Php, and have the client to download that file.
How would i do that?
Essentially the client should be able to download the file from the server.
This is the best way to do it, supposing you don't want the user to see the real URL of the file.
<?php
$filename="download.txt";
header("Content-disposition: attachment;filename=$filename");
readfile($filename);
?>
Additionally, you could protect your files with mod_access.
In addition to the data already posted, there is a header you might want to try.
Its only a suggestion to how its meant to be handled, and the user agent can chose to ignore it, and simply display the file in the window if it knows how:
<?php
header('Content-Type: text/plain'); # its a text file
header('Content-Disposition: attachment'); # hit to trigger external mechanisms instead of inbuilt
See Rfc2183 for more on the Content-Disposition header.
PHP has a number of very simplistic, C-like functions for writing to files. Here is an easy example:
<?php
// first parameter is the filename
//second parameter is the modifier: r=read, w=write, a=append
$handle = fopen("logs/thisFile.txt", "w");
$myContent = "This is my awesome string!";
// actually write the file contents
fwrite($handle, $myContent);
// close the file pointer
fclose($handle);
?>
It's a very basic example, but you can find more references to this sort of operation here:
PHP fopen
If you set the content type to application/octet-stream, the browser will ALWAYS offer file as a download, and will never attempt to display it internally, no matter what type of file it is.
<?php
filename="download.txt";
header("Content-type: application/octet-stream");
header("Content-disposition: attachment;filename=$filename");
// output file content here
?>
Just post a link on the site to http://example.com/textfile.php
And in that PHP file you put the following code:
<?php
header('Content-Type: text/plain');
print "The output text";
?>
That way you can create the content dynamic (from a database)...
Try to Google to oter "Content-Type" if this one is not the one you are looking for.

Categories