Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nomade

macrumors member
Original poster
Dec 2, 2006
72
0
I have use a very close version of this script in many web site but this one doesn't work. It's a mailing list with file attached.

Code:
// I drop the file on the server 
if(!empty($_FILES['fichier'])){
$nom = substr($_FILES['fichier']['name'], strripos($_FILES['fichier']['name'], '.'));
$extension= substr($nom, 1, 3);
$connection = ftp_connect('cld-charlevoix.org') or die("connection impossible");
ftp_login($connection, $user, $password) or die("user erreur ou mot de passe");
ftp_put($connection,("/public_html/admin/attachement/".$_FILES['fichier']['name']),$fichier,FTP_BINARY) or die("le transfert marche pas");
ftp_close($connection);
echo "go";
}

// then i create the script for the mailing
if(isset($_POST['groupe'])){
//---------------------------------------------------------- ramasse le texte et le titre
$sql_texte2=mysql_query("SELECT * FROM texte WHERE id_texte='18'");
$ligne_texte2=mysql_fetch_array($sql_texte2);
$titre_texte2=$ligne_texte2['titre']; // le titre du message

//------------------------------------------
foreach($_POST['groupe'] as $lesgroupes) {
if($_POST['groupe'][0] !='tous'){ 
$sql_listeenvoi=mysql_query("SELECT * FROM listeenvoi where groupe='".$lesgroupes."' and actif='oui' ORDER BY courriel ASC");
 }
else{
$sql_listeenvoi=mysql_query("SELECT * FROM listeenvoi where actif='oui' ORDER BY courriel ASC");
}
$i=1;

while($ligne_listeenvoi=mysql_fetch_array($sql_listeenvoi)){
$to = "".$ligne_listeenvoi['courriel']."";

//------------------------------------------
$sujet = $titre_texte2; 
$delimiteur=md5(uniqid(rand()));
$entete = "MIME-version: 1.0\n"; 
$entete  .= "From:info@cld-charlevoix.org\n"; 
$entete .= "Content-type: multipart/mixed; boundary=".$delimiteur." \n ";
$entete .= " \n";
$sujet = $titre_texte; 
$message ="--".$delimiteur."\n";
$message .= "Content-type: text/html; charset= \"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding:8bit\n";
$message .= "\n";
$message .="Hello";

$message .= "\n";
$message .="--".$delimiteur." \n";
if(!empty($_FILES['fichier']['name'])){
$fichier ="attachement/".$_FILES['fichier']['name']."";
$attache = file_get_contents($fichier);
$attache = chunk_split(base64_encode($attache));
$message .="--".$delimiteur." \n";
$message .="Content-Type: application/".$extension."; name=".$fichier." \n";
$message .="Content-Transfer-Encoding: Base64\n";
$message .="Content-Disposition: inline; filename=".$fichier." \n";
$message .= "\n";
$message .= $attache." \n";
$message .= "\n";

$message .="--".$delimiteur." \n";
}
mail($to,$sujet,$message,$entete, "-flamuse@lamuse.com");
}}}
echo "$to<br>";
?>

The file is indeed on the server but the image/file is not interpreted correctly.
Any suggestion will be welcomed
 

SrWebDeveloper

macrumors 68000
Dec 7, 2007
1,871
3
Alexandria, VA, USA
Not a clue what you mean by "not interpreted" so if this advice does not help you need to explain exactly what you mean and what happened when you run the script.

In PHP uploaded files are stored in a temporary location and must be copied to a real folder to attach or FTP, whatever.

I don't see something similar in concept to this, which moves the uploaded temp file to a REAL folder with the proper name in this example called upload. Edit to suit your own needs:

PHP:
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

:D
 

nomade

macrumors member
Original poster
Dec 2, 2006
72
0
Not a clue what you mean by "not interpreted" so if this advice does not help you need to explain exactly what you mean and what happened when you run the script.

In PHP uploaded files are stored in a temporary location and must be copied to a real folder to attach or FTP, whatever.

I don't see something similar in concept to this, which moves the uploaded temp file to a REAL folder with the proper name in this example called upload. Edit to suit your own needs:

:D

Oh sorry for the bad explanation, when I said not interpreted I mean the image attached is show in the email as caracters within boundary but not as an image.

The file is already upload to the server :
Code:
if(!empty($_FILES['fichier'])){
$nom = substr($_FILES['fichier']['name'], strripos($_FILES['fichier']['name'], '.'));
$extension= substr($nom, 1, 3);
$connection = ftp_connect('cld-charlevoix.org') or die("connection impossible");
ftp_login($connection, $user, $password) or die("user erreur ou mot de passe");
ftp_put($connection,("/public_html/admin/attachement/".$_FILES['fichier']['name']),$fichier,FTP_BINARY) or die("le transfert marche pas");
ftp_close($connection);
echo "go";
}

Thanks for your help :)
 

SrWebDeveloper

macrumors 68000
Dec 7, 2007
1,871
3
Alexandria, VA, USA
The actual file is stored in $_FILES["file"]["tmp_name"] and you must move it as in my example code. Then you can FTP or attach via email, etc. I already told you I don't see the move command anywhere in your code.

Did you login to the FTP server and not only confirm the file name is there but that it's the actual image? Download it manually to be sure. To me looks like you're uploading a file name, not a binary. And in your email code, no move command so binary and it would end up the way you see it now.

Anyone else following this please correct me if I'm wrong on this or missing something obvious!
 

nomade

macrumors member
Original poster
Dec 2, 2006
72
0
The actual file is stored in $_FILES["file"]["tmp_name"] and you must move it as in my example code. Then you can FTP or attach via email, etc. I already told you I don't see the move command anywhere in your code.

Did you login to the FTP server and not only confirm the file name is there but that it's the actual image? Download it manually to be sure. To me looks like you're uploading a file name, not a binary. And in your email code, no move command so binary and it would end up the way you see it now.

Anyone else following this please correct me if I'm wrong on this or missing something obvious!

The file is move to physical folder here:
Code:
ftp_put($connection,("/public_html/admin/attachement/".$_FILES['fichier']['name']

I do log in ftp here :
Code:
$connection = ftp_connect('cld-charlevoix.org') or die("connection impossible");
ftp_login($connection, $user, $password) or die("user erreur ou mot de passe");

yes i did check the image on the server and it's in fact an image.

Nomade
 

SrWebDeveloper

macrumors 68000
Dec 7, 2007
1,871
3
Alexandria, VA, USA
I mean this respectfully, but the code you posted is how to ftp a file in PHP but you must have some other code you'r not telling us about that places the file in the source server path specified PRIOR to FTP based on your code as posted here.

Look at this example written by someone else on php.net which I am copying to show you they're using the 'tmp_name' key of the $_FILES array to create the variable that stores the actual file:

PHP:
EXAMPLE:

When sending files using a form and PHP, make sure that all the data (text files etc...) are retreived with $_POST, and files (smiley.png, compression.zip, etc...) are retreived with $_FILES.

here's what your start of a results.php file might look like:
<?PHP
   $myName = $_POST['name']; //This will copy the text into a variable
   $myFile = $_FILES['file_name']; // This will make an array out of the file information that was stored.
?>

Now when it comes to transmitting that information...

<?PHP
        $destination_path = "src/bin/"; 

//where you want to throw the file on the webserver (relative to your login dir)

    $destination_file = $destination_path."img.jpg";

//This will create a full path with the file on the end for you to  use, I like splitting the variables like this in case I need to use on on their own or if I'm dynamically creating new folders.

        $file = $myFile['tmp_name'];

//Converts the array into a new string containing the path name on the server where your file is.

    $upload = ftp_put($conn_id, $destination_file, $file, FTP_BINARY);// upload the file
    if (!$upload) {// check upload status
        echo "FTP upload of $destination_file has failed!";
    } else {
        echo "Uploaded $file to $conn_id as $destination_file";
    }
?>

As to email, similar situation, but I already gave you example code for that.

I want to be clear on this -- $_FILES['file_name'] does not store the actual file when processing server side immediately after an upload. $_FILES['tmp_name'] does and is physically stored in the temp path on your server unless you move it, otherwise when the PHP script ends it is removed from the server.

I'll leave it to others to jump in, I've done all I can do on this one.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.