/    Sign up×
Community /Pin to ProfileBookmark

fopen() file from another website

Hi,

I’ve written a short php page that is supposed to simply email a csv file. The page works fine when the file is located in the same folder as the php file, but I can’t get it to work when the file is located on another website.

Here is a live version of the page: [url]http://10stoptours.com/emailer/sendemail.php[/url]

Please help!

Here is the code:

[code=php]
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
echo date(“D M d, Y G:i”).”.csv”;

// Settings
$name = “Registrar”;
$email = “my email [email protected]”;
$to = “$name <$email>”;
$from = “Registrar Sender “;
$subject = “Here is your attachment”;
$mainMessage = “Hi, here’s the file.”;
//$fileatt = “./WN2015_open.csv”;
$fileatt = “http://www.ro.umich.edu/timesched/pdf/WN2015_open.csv”;
$fileatttype = “application/csv”;
$fileattname = date(“D M d, Y G:i”).”.csv”;
$headers = “From: $from”;

// File
$file = fopen($fileatt, ‘rb’);
$data = fread($file, filesize($fileatt));
fclose($file);

// This attaches the file
$semi_rand = md5(time());
$mime_boundary = “==Multipart_Boundary_x{$semi_rand}x”;
$headers .= “nMIME-Version: 1.0n” .
“Content-Type: multipart/mixed;n” .
” boundary=”{$mime_boundary}””;
$message = “This is a multi-part message in MIME format.nn” .
“-{$mime_boundary}n” .
“Content-Type: text/plain; charset=”iso-8859-1n” .
“Content-Transfer-Encoding: 7bitnn” .
$mainMessage . “nn”;

$data = chunk_split(base64_encode($data));
$message .= “–{$mime_boundary}n” .
“Content-Type: {$fileatttype};n” .
” name=”{$fileattname}”n” .
“Content-Disposition: attachment;n” .
” filename=”{$fileattname}”n” .
“Content-Transfer-Encoding: base64nn” .
$data . “nn” .
“-{$mime_boundary}-n”;

// Send the email
if(mail($to, $subject, $message, $headers)) {

echo “The email was sent.”;

}
else {

echo “There was an error sending the mail.”;

}
?>
</body>
</html>
[/code]

Thanks,
Alex

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 31.2014 — If you just want to read the entire file into one variable, use file_get_contents() instead of opening a handle, reading the handle, then closing the handle. That being said, depending on how portable your script needs to be and what your PHP security settings are, you may want to use the cURL functions to access remote files via HTTP instead of using the file functions (which are best used on the local file system).
Copy linkTweet thisAlerts:
@ablatyauthorOct 31.2014 — If you just want to read the entire file into one variable, use file_get_contents() instead of opening a handle, reading the handle, then closing the handle. That being said, depending on how portable your script needs to be and what your PHP security settings are, you may want to use the cURL functions to access remote files via HTTP instead of using the file functions (which are best used on the local file system).[/QUOTE]

Thanks for the quick reply. Do you know how I could modify my php to use a cURL? I'm pretty new to PHP and never used cURL before.

Thanks,

Alex
Copy linkTweet thisAlerts:
@NogDogOct 31.2014 — If we assume $url is the full URL to the file you want, at its most basic:
[code=php]
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // return the actual content
$data = curl_exec($curl);
curl_close($curl);
[/code]

Things become a bit more complex if you need to exchange cookies, make post requests, etc., but that may be all you need for now.
Copy linkTweet thisAlerts:
@ablatyauthorNov 01.2014 — Worked perfectly. Thanks!
×

Success!

Help @ablaty spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...