SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    $_FILES comes up empty

    Hi all!

    I have a page with a form that lets users fill some inputs and submit up to 3 image files.
    When form is submitted, all the input fields and the 3 images (as attachments) are sent to a mailbox.

    It works fine if I call the page alone.

    But then I rearranged the site and the page is included when a link is clicked, so I have index.php that includes page1.php (that contains the submission form).
    The mail gets sent, with all the fields, but not the files in attachment.
    I tried print_r($_FILES) but the array is empty.
    Is this some kind of scope problem?

    My Form (page1.php):
    HTML Code:
    <form id="frm5" name="frm5" enctype="multipart/form-data" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
    		<fieldset class="fldset">
    			<legend class="fldsetlgnd">Submeter Dados e Fotos para O Teu Carro</legend>
    			<table border="0">
    				<tr>
    					<td>Nome:</td>
    					<td>
    						<input type="text" id="txtnomeotc" name="txtnomeotc" size="50" value="<?php echo $nomeotc; ?>" class="txt" />
    					</td>
    				</tr>
    				<tr>
    					<td>Telefone:</td>
    					<td>
    						<input type="text" id="txttlfotc" name="txttlfotc" size="12" value="<?php echo $tlfotc; ?>" class="txt2" />
    					</td>
    				</tr>
    				<tr>
    					<td>Email:</td>
    					<td>
    						<input type="text" id="txtemailotc" name="txtemailotc" size="50" value="<?php echo $emailotc; ?>" class="txt" />
    					</td>
    				</tr>
    				<tr>
    					<td>Marca:</td>
    					<td>
    						<input type="text" id="txtmarcaotc" name="txtmarcaotc" size="50" value="<?php echo $marcaotc; ?>" class="txt" />
    					</td>
    				</tr>
    				<tr>
    					<td>Modelo:</td>
    					<td>
    						<input type="text" id="txtmodelootc" name="txtmodelootc" size="50" value="<?php echo $modelootc; ?>" class="txt" />
    					</td>
    				</tr>
    				<tr>
    					<td>Foto 1:</td>
    					<td>
    						<input type="file" name="txtfoto1otc" size="30" value="" class="btn" />
    					</td>
    				</tr>
    				<tr>
    					<td>Foto 2:</td>
    					<td>
    						<input type="file" name="txtfoto2otc" size="30" value="" class="btn" />
    					</td>
    				</tr>
    				<tr>
    					<td>Foto 3:</td>
    					<td>
    						<input type="file" name="txtfoto3otc" size="30" value="" class="btn" />
    					</td>
    				</tr>
    
    				<tr>
    					<td colspan="2">&nbsp;</td>
    				</tr>
    				<tr>
    					<td colspan="2" align="center"><input type="submit" id="btnSubmit5" name="btnSubmit5" value="Submeter" class="btn" /></td>
    				</tr>				
    			</table>
    		</fieldset>
    		</form>

    and my code that process the form (on index.php):

    PHP Code:
    if(isset($_POST['btnSubmit5']))
    {
        
    $to 'mramos@motorpress.pt';
        
    $nome $_POST['txtnomeotc'];
        
    $tlf $_POST['txttlfotc'];
        
    $email $_POST['txtemailotc'];
        
    $marca $_POST['txtmarcaotc'];
        
    $modelo $_POST['txtmodelootc'];
        
    $assunto 'Dados e Fotos para «O Teu Carro»';
        
    $msgtxt "&laquo;Dados e Fotos para O Teu Carro&raquo;<br /><br />"// Message that the email has in it
        

        
    $nome strip_tags($nome);
        
    $tlf strip_tags($tlf);
        
    $email strip_tags($email);
        
    $marca strip_tags($marca);
        
    $modelo strip_tags($modelo);


        
    $msgtxt .= '<table>';
        
    $msgtxt .= '<tr><td>Nome:</td><td>'.$nome.'</td></tr>';
        
    $msgtxt .= '<tr><td>Telefone:</td><td>'.$tlf.'</td></tr>';
        
    $msgtxt .= '<tr><td>Email:</td><td>'.$email.'</td></tr>';
        
    $msgtxt .= '<tr><td>Marca:</td><td>'.$marca.'</td></tr>';
        
    $msgtxt .= '<tr><td>Modelo:</td><td>'.$modelo.'</td></tr>';
        
    $msgtxt .= '</table>';

        
        
    // generate a random string to be used as the boundary marker
        
    $semi_rand md5(time());
        
    $mime_boundary "==Multipart_Boundary_x{$semi_rand}x";

        
    $headers "From: $email\r\n" .    
        
    "MIME-Version: 1.0\r\n" .
        
    "Content-Type: multipart/mixed;\r\n" .
        
    " boundary=\"{$mime_boundary}\"";

        
        
    $msgtxt .= "This is a multi-part message in MIME format.\n\n" .
        
    "--{$mime_boundary}\n" .
        
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
        
    "Content-Transfer-Encoding: 7bit\n\n" .
        
    $msgtxt "\n\n";

         echo 
    '<b>Files: </b>'.count($_FILES).'<br />';
        foreach(
    $_FILES as $fileatt)
        {
            if(
    $fileatt['name'] != '')
            {
                
    $fileatt_type $fileatt['type']; // File Type
                
    $fileatt_name $fileatt['name']; // Filename that will be used for the file as the attachment
                
    $fileatt_size $fileatt['size']; // File size
                
    $fileatt_tmp_name $fileatt['tmp_name']; // Upload file
        
                
    echo 'Type: '.$fileatt_type.'<br />';
                echo 
    'Name: '.$fileatt_name.'<br />';
                echo 
    'Size: '.$fileatt_size.'<br />';
                echo 
    'TMP Name: '.$fileatt_tmp_name.'<br />';
            
            
                if ( (
    $fileatt_type == 'image/gif') || ($fileatt_type == 'image/jpeg') || ($fileatt_type == 'image/png') || ($fileatt_type == 'image/tiff') || ($fileatt_type == 'image/bmp') )
                {
                    if(
    $fileatt_size 512000)
                    {
                        
    //die("Sorry but the email could not be sent. File size must be under 500KB!");
                        
    $mensagem 'A sua mensagem não pode ser enviada. Os ficheiros não devem ultrapassar os 500KB cada!';
                    }
                    else
                    {
                        
    // if the upload succeded, the file will exist
                        
    if(file_exists($fileatt_tmp_name))
                        {
                            
    // check to make sure that it is an uploaded file and not a system file
                            
    if(is_uploaded_file($fileatt_tmp_name))
                            {
                                
    $file fopen($fileatt_tmp_name,'rb');
                                
    $data fread($file,filesize($fileatt_tmp_name));
                                
    fclose($file);
                        
                                
    $data chunk_split(base64_encode($data));
                    
                            } 
    // end is_uploaded_file
                        
                            
    $msgtxt .= "--{$mime_boundary}\n" .
                            
    "Content-Type: {$fileatt_type};\n" .
                            
    " name=\"{$fileatt_name}\"\n" .
                            
    "Content-Disposition: attachment;\n" .
                            
    " filename=\"{$fileatt_name}\"\n" .
                            
    "Content-Transfer-Encoding: base64\n\n" .
                            
    $data "\n\n" .
                            
    "--{$mime_boundary}\n";
                    
                        } 
    // end file_exists
                    
                        
    unset($data);
                        unset(
    $file);
                        unset(
    $fileatt);
                        unset(
    $fileatt_type);
                        unset(
    $fileatt_name);
                        unset(
    $fileatt_size);
                        unset(
    $fileatt_tmp_name);
            
                    } 
    //end file size check
            
                

                else 
                {
                    
    //die("Sorry but the email could not be sent. Wrong file type!");
                    
    $mensagem 'A sua mensagem não pode ser enviada. O tipo de ficheiro não é permitido!';
            
                } 
    // end filetype check
            
    // end file count
        
    // end foreach FILES

        

        
    $enviar mail($to$assunto$msgtxt$headers);
        
    $mensagem =  $enviar "Dados Enviados" "Dados NÃO Enviados";

    // end btnSubmit5 

    Another problem with this emailing script is that along with the image as attachment I get an empty file like 'ATT9353397.txt' at my work email with Outlook, but if I send it to any other webmail system it doesn't appear.

    Thankx for your help.
    Yours truely
    Mário Ramos

  2. #2
    SitePoint Zealot Gman's Avatar
    Join Date
    Jan 2002
    Location
    Sarasota, FL
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you need to change the name of your file input names to an array:

    name="txtfotootc[]"

    edit: make sure you name them with the same name!

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That didn't fix it...
    Yours truely
    Mário Ramos

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to change the uploading script around the looping.
    PHP Code:
    $files $_FILES['txtfotootc'];
    for(
    $i 0$i count($files); $i++){
        
    $filename $files[$i]['name'];
        
    #some codes are here

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Still no luck.

    I don't think the problem is with the input field (file) naming, because it works if I call the file on it's own...

    I think it's some kind of scope problem.
    Yours truely
    Mário Ramos

  6. #6
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    *Bump*
    Yours truely
    Mário Ramos

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Possibly an obvious question but where did you try print_r($_FILES)?!

    When I copy and paste the code exactly as is I get:
    Files: 3
    Type: image/jpeg
    Name: example.jpg
    Size: 26634
    TMP Name: C:\WINDOWS\Temp\php2EC.tmp
    Type: image/jpeg
    Name: NV_WP_Green4-4x3.jpg
    Size: 546290
    TMP Name: C:\WINDOWS\Temp\php2ED.tmp
    Type: image/jpeg
    Name: NV_WP_Green4-12x10.jpg
    Size: 391602
    TMP Name: C:\WINDOWS\Temp\php2EE.tmp
    as the output - which is what I would expect.

    What do you get?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes,

    I tried print_r($_FILES). I get error 4 -> No file was uploaded.

    If I have the form and the processing script on the same file, there's no problem.
    But I want the form on 1 page and included in index.php.
    The index.php has the form processing script (mail script).
    Yours truely
    Mário Ramos

  9. #9
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    hmmm, when I move the processing part onto a seperate page entirely I still get:
    PHP Code:
    Array
    (
        [
    txtfoto1otc] => Array
            (
                [
    name] => NV_WP_Green4-4x3.jpg
                
    [type] => image/jpeg
                
    [tmp_name] => C:\WINDOWS\Temp\php32E.tmp
                
    [error] => 0
                
    [size] => 546290
            
    )

        [
    txtfoto2otc] => Array
            (
                [
    name] => NV_WP_Green4-16x9.jpg
                
    [type] => image/jpeg
                
    [tmp_name] => C:\WINDOWS\Temp\php32F.tmp
                
    [error] => 0
                
    [size] => 626318
            
    )

        [
    txtfoto3otc] => Array
            (
                [
    name] => NV_WP_Green1-12x10.jpg
                
    [type] => image/jpeg
                
    [tmp_name] => C:\WINDOWS\Temp\php330.tmp
                
    [error] => 0
                
    [size] => 117635
            
    )

    )

    Files3
    Type
    image/jpeg
    Name
    NV_WP_Green4-4x3.jpg
    Size
    546290
    TMP Name
    C:\WINDOWS\Temp\php32E.tmp
    Type
    image/jpeg
    Name
    NV_WP_Green4-16x9.jpg
    Size
    626318
    TMP Name
    C:\WINDOWS\Temp\php32F.tmp
    Type
    image/jpeg
    Name
    NV_WP_Green1-12x10.jpg
    Size
    117635
    TMP Name
    C:\WINDOWS\Temp\php330.tmp 
    What else is on the processing page that could effect the FILES array?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  10. #10
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Angry

    I'm going crazy with this.

    If I call the webpage in firefox, the form and images get sent, but in IE, only the form data get sent not the images, although everthing is ok.


    I too get the file(s) name, type and size.
    Yours truely
    Mário Ramos

  11. #11
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well, I recoded the form and the processing script.

    I couldn't find what was wrong but it finally works.

    Thankx for all your help.
    Yours truely
    Mário Ramos

  12. #12
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    The issue was most probably this line:
    PHP Code:
    <form id="frm5" name="frm5" enctype="multipart/form-data" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
    The line would output:
    PHP Code:
    <form id="frm5" name="frm5" enctype="multipart/form-data" method="post" action=""
    Depending on what browser you use the line above will work or not. And if it dont work, exactly what you describe happens.

    Remember to always use echo or print etc when you want to output something from php.

  13. #13
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Humm,

    I remember looking at the generated HTML and seeing an empty action. Maybe that was it.

    Thank you.
    Yours truely
    Mário Ramos


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •