Jump to content

Recommended Posts

Hello,

 

Having a bit of a problem with .wmv and ot her media files..

 

Basically, I am trying to upload the contents of a file ($file) using the following method:

 

if(file_exists($file))

{

  $fh = fopen($file, 'rb');

  print(fread($fh, filesize($file)));

}

 

Trouble is, my $file is of the form http://myserver/00000001.wmv (or .jpg or .mp3).

 

Php does not seem to be recognising the URL of the file as a file with the file_exists function. Even if I skip this and go to fopen, again, it thinks nothing is there  :(

 

But if you went to http://myserver/00000001.wmv though (for example) the file plays fine.

 

Please help - this has been driving me up the wall all day!! ???

 

Thanks,

 

:)

Link to comment
https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/
Share on other sites

Then I would use the HTTP status code to check the file exists. From the url http://yoursite,com/files/000.wmv - if it returns a 200 then the url is fine. If it returns a 404 then the file does not exist.

 

Test it out here http://www.seoconsultants.com/tools/headers.asp

Then write a PHP script to do it, not that hard.

Hi,

 

Yes, the file definitely exists - it's just a case of opening it from an alternate URL (for security reasons).

 

The site will be dealing with 1000s of images and pictures so downloading to a file on the server is not an option...is there really not a file_exists and fopen equivelent for a url?

 

I'm a bit of a newbie to this so apologies if my questions seem a bit obvious!

 

Thanks

Yes you can use fopen() but only if the remote server allows this.

What is wrong with checking the HTTP header with the URL as I said.

 

Heres some code. Try it out:

<?php
function httpHeader($url) {
	$urlParts = parse_url($url);

	$fp	= fsockopen($urlParts['host'],80,$errno,$errstr,10);
	$out = "GET ".$url." HTTP/1.1\r\n"; 
	$out.= "Host: ".$urlParts['host']."\r\n"; 
	$out.= "Connection: Close\r\n\r\n";
	fwrite($fp,$out);
	$content = fgets($fp);

	return $content;
}


$url = "http://www.google.co.uk/intl/en_uk/images/logo.gif";
print httpHeader($url);
?>

Still no joy - I swear I'm doing something daft  :-\

 

The httpHeader function worked fine - I got the 'HTTP/1.1 200 OK' reply for my URL.

 

And allow_url_fopen is on in my php.ini file

 

But when I try to add the following code to open the image, no joy:

 

  $url = "http://www.google.co.uk/intl/en_uk/images/logo.gif";

 

  $fh = fopen($url, 'rb');

  print(fread($fh, filesize($url)));

 

Any ideas?! Thanks for the help!

function getSizeFile($url) { 
    if (substr($url,0,4)=='http') { 
        $x = array_change_key_case(get_headers($url, 1),CASE_LOWER); 
        if ( strcasecmp($x[0], 'HTTP/1.1 200 OK') != 0 ) { $x = $x['content-length'][1]; } 
        else { $x = $x['content-length']; } 
    } 
    else { $x = @filesize($url); } 

    return $x; 
}
print getSizeFile("http://www.google.co.uk/intl/en_uk/images/logo.gif");

Cool, thanks. I had something similar (but not quite as good  ;) ) to get the filesize.

 

I think I'm nearly there (we hope!). Below is the code but instead of outputing the image I get 'GIF89a' .... is print the wrong function to use here?

 

[attachment deleted by admin]

Think I've solved the last bit myself! To download the image, I use the following code:

 

$file = 'http://www.google.co.uk/intl/en_uk/images/logo.gif'; 

 

    header('Content-Description: File Transfer');

    header('Content-Type: application/octet-stream');

    header('Content-Disposition: attachment; filename='.basename($file));

    header('Content-Transfer-Encoding: binary');

    header('Expires: 0');

    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

    header('Pragma: public');

    header('Content-Length: ' . $filesize);

    ob_clean();

    flush();

    readfile($file);

    exit;

 

That seems to download the file  :)

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.