Jump to content

[SOLVED] Upload files and write to database... how to do this?


lopes_andre

Recommended Posts

Hi,

 

I have an HTML form and a PHP file to upload files to the server and write the direction of the file in the server to the database. My problem is that I'am uploading the files in one way that I don't know wich files the user have uploaded...

 

I explain better...

 

here is my form:

 

<FORM METHOD="POST" NAME="formmail" ACTION="reply.php" enctype=multipart/form-data>

<input type=hidden name="id_anuncio" value="<?PHP echo $id_anuncio; ?>">

Curriculum: <input type=file name='images[]' class='bginput'>  <br />
Portfolio: <input type=file name='images[]' class='bginput'> <br />
Presentation Letter: <input type=file name='images[]' class='bginput'> <br /> 
Other: <input type=file name='images[]' class='bginput'> <br />

<BR>
<INPUT TYPE="submit" NAME="BUTTON" VALUE="Send Form"><INPUT TYPE="reset" VALUE="Reset" Form">
</FORM>

 

Ok, the user could send 1, 2, 3 ou 4 files. But I never know how many files the user will send... For example, the user could only send the "Presentation Letter"

 

 

To upload the files I use this function:

 

function uploadFile() {
global $attachments;
while(list($key,$value) = each($_FILES[images][name]))
			{

				if(!empty($value))
				{
						$filename = $value;
						array_push($attachments, $filename);
						$dir = "/home/uploads/anuncio_completo/$filename";

						chmod("/home/uploads/anuncio_completo",0777);


						$original_filename = $filename;
						$filemane_lenght = strlen($original_filename); // see the lenght of the string
						$filename_wo_type = $filemane_lenght - 4;
						$filename_wo_type2 = substr($original_filename, 0, $filename_wo_type);
						$filename_type = substr($filename, -4);
						$timestamp = time();
						$new_filename = $filename_wo_type2 . $timestamp . $filename_type;

						$dir2 = "/home/uploads/anuncio_completo/$new_filename";


				        $success = copy($_FILES[images][tmp_name][$key], $dir2);
				         
				}

			}
									    
				           if ($success) {
							echo " Files Uploaded Successfully<BR>";



							SendIt();

								}else {
										exit("Sorry the server was unable to upload the files...");
									}

}

 

Ok, I upload the files using the uploadFile function, but my problem is that I need to write to database the location of the uploaded files to specific fields... And I need to add to this function an INSERT statement to the table that stores the location of the files...

 

Here is the example of the database table to receive the location of the files:

 

id_anuncio INT(15)

curriculum VARCHAR(70)

portfolio VARCHAR(70)

presentation_letter VARCHAR(70)

other VARCHAR(70)

 

My question... How I modify the function and the form to know exactly wich files the user have upload to write to the database the correct paths to the fields in the database.

 

 

 

Best Regards,

André.

two options...use different names for input fields:

Curriculum: <input type="file" name="image_curriculum" class="bginput">  <br />
Portfolio: <input type="file" name="image_portfolio" class="bginput"> <br />
Presentation Letter: <input type="file" name="image_presentation" class="bginput"> <br />
Other: <input type="file" name="image_other" class="bginput"> <br />

or use associative arrays instead of numeric:

Curriculum: <input type="file" name="image[curriculum]" class="bginput">  <br />
Portfolio: <input type="file" name="image[portfolio]" class="bginput"> <br />
Presentation Letter: <input type="file" name="image[presentation]" class="bginput"> <br />
Other: <input type="file" name="image[other]" class="bginput"> <br />

edit: with the above, instead of $_FILES['images'] being a numeric array, it will be an associative array with the keys you list in the name attribute

two options...use different names for input fields:

Curriculum: <input type="file" name="image_curriculum" class="bginput">  <br />
Portfolio: <input type="file" name="image_portfolio" class="bginput"> <br />
Presentation Letter: <input type="file" name="image_presentation" class="bginput"> <br />
Other: <input type="file" name="image_other" class="bginput"> <br />

or use associative arrays instead of numeric:

Curriculum: <input type="file" name="image[curriculum]" class="bginput">  <br />
Portfolio: <input type="file" name="image[portfolio]" class="bginput"> <br />
Presentation Letter: <input type="file" name="image[presentation]" class="bginput"> <br />
Other: <input type="file" name="image[other]" class="bginput"> <br />

edit: with the above, instead of $_FILES['images'] being a numeric array, it will be an associative array with the keys you list in the name attribute

 

Ok, If I choose associative arrays, how can I rewrite the function?

 

The actual funciton is:

 

$attachments = array();

function uploadFile() {
global $attachments;
while(list($key,$value) = each($_FILES[images][name]))
			{

				if(!empty($value))
				{
						$filename = $value;
						array_push($attachments, $filename);
						$dir = "/home/emp1001/uploads/anuncio_completo/$filename";

						chmod("/home/emp1001/uploads/anuncio_completo",0777);
						$original_filename = $filename;
						$filemane_lenght = strlen($original_filename); // see the lenght of the string
						$filename_wo_type = $filemane_lenght - 4;
						$filename_wo_type2 = substr($original_filename, 0, $filename_wo_type);
						$filename_type = substr($filename, -4);
						$timestamp = time();
						$new_filename = $filename_wo_type2 . $timestamp . $filename_type;

						$dir2 = "/home/emp1001/uploads/anuncio_completo/$new_filename";


				        $success = copy($_FILES[images][tmp_name][$key], $dir2);
				         
				}

			}
									    
				           if ($success) {
							echo " Files Uploaded Successfully<BR>";


							SendIt();

								}else {
										exit("Sorry the server was unable to upload the files...");
									}

}

 

 

The goal is to make an INSERT statement like this inside the function:

 

insert into table (curriculum, portfolio, presentation_letter, other) values ('".$new_filename['curriculum']."', '".$new_filename['portfolio']."', '".$new_filename['presentation_letter']."', '".$new_filename['other']."');

 

Please give me clues how to modify the function.

 

 

Best Regards,

André.

try this:

<?php
$attachments = array ();

function uploadFile() {
  global $attachments;
  $allowed = array('curriculum','portfolio','presentation_letter','other');
  foreach (array_keys($_FILES['images']['name']) as $type ) {
    if($_FILES['name'][$type] && in_array($type,$allowed)){
      if (empty ($_FILES['error'][$type])) {
        $filename = $_FILES['name'][$type];
        array_push($attachments, $filename);
        $dir = "/home/emp1001/uploads/anuncio_completo/$filename";
  
        chmod("/home/emp1001/uploads/anuncio_completo", 0777);
        $original_filename = $filename;
        $filemane_lenght = strlen($original_filename); // see the lenght of the string
        $filename_wo_type = $filemane_lenght -4;
        $filename_wo_type2 = substr($original_filename, 0, $filename_wo_type);
        $filename_type = substr($filename, -4);
        $timestamp = time();
        $new_filename = $filename_wo_type2 . $timestamp . $filename_type;
  
        $dir2 = "/home/emp1001/uploads/anuncio_completo/$new_filename";
  
        if(move_uploaded_file($_FILES['tmp_name'][$type], $dir2))
          $new_filename[$type] = $filename;
      }else{
        echo "The $type file failed with code: {$_FILES['error'][$type]}<br />";
      }
    }
  }
  if(count($attachments)){
    echo " Files Uploaded Successfully<BR>";
    $sql = "insert into table (curriculum, portfolio, presentation_letter, other) values ('".$new_filename['curriculum']."', '".$new_filename['portfolio']."', '".$new_filename['presentation_letter']."', '".$new_filename['other']."');";
    SendIt();
  } else {
    exit ("Sorry the server was unable to upload the files...");
  }

}
?>

try this:

<?php
$attachments = array ();

function uploadFile() {
  global $attachments;
  $allowed = array('curriculum','portfolio','presentation_letter','other');
  foreach (array_keys($_FILES['images']['name']) as $type ) {
    if($_FILES['name'][$type] && in_array($type,$allowed)){
      if (empty ($_FILES['error'][$type])) {
        $filename = $_FILES['name'][$type];
        array_push($attachments, $filename);
        $dir = "/home/emp1001/uploads/anuncio_completo/$filename";
  
        chmod("/home/emp1001/uploads/anuncio_completo", 0777);
        $original_filename = $filename;
        $filemane_lenght = strlen($original_filename); // see the lenght of the string
        $filename_wo_type = $filemane_lenght -4;
        $filename_wo_type2 = substr($original_filename, 0, $filename_wo_type);
        $filename_type = substr($filename, -4);
        $timestamp = time();
        $new_filename = $filename_wo_type2 . $timestamp . $filename_type;
  
        $dir2 = "/home/emp1001/uploads/anuncio_completo/$new_filename";
  
        if(move_uploaded_file($_FILES['tmp_name'][$type], $dir2))
          $new_filename[$type] = $filename;
      }else{
        echo "The $type file failed with code: {$_FILES['error'][$type]}<br />";
      }
    }
  }
  if(count($attachments)){
    echo " Files Uploaded Successfully<BR>";
    $sql = "insert into table (curriculum, portfolio, presentation_letter, other) values ('".$new_filename['curriculum']."', '".$new_filename['portfolio']."', '".$new_filename['presentation_letter']."', '".$new_filename['other']."');";
    SendIt();
  } else {
    exit ("Sorry the server was unable to upload the files...");
  }

}
?>

 

Hi, thanks for your help first of all.

 

For now I can't upload the files. Gives me the error "Sorry the server was unable to upload the files...".

 

How can I debug the function? I don't know how to call the Debug variable with the name of the file.

 

Something like this?

######## DEBUG CONFIGURATION #######

$_FILES['curriculum'] = "C:\Documents and Settings\andre\My Documents\CV_AG.doc";

####################################

 

Best Regards,

André.

ok...more tweaking...try this now:

function uploadFile() {
  global $attachments;
  $upload_dir = "/home/emp1001/uploads/anuncio_completo/";
  chmod($upload_dir, 0775);
  
  $files = array();
  $allowed = array('curriculum','portfolio','presentation_letter','other');
  foreach (array_keys($_FILES['image']['name']) as $type ) {
    if($_FILES['image']['name'][$type] && in_array($type,$allowed)){
      if (empty ($_FILES['image']['error'][$type])) {
        $filename = $_FILES['image']['name'][$type];
        array_push($attachments, $filename);
          
        $original_filename = $filename;
        $filemane_lenght = strlen($original_filename); // see the lenght of the string
        $filename_wo_type = $filemane_lenght -4;
        $filename_wo_type2 = substr($original_filename, 0, $filename_wo_type);
        $filename_type = substr($filename, -4);
        $timestamp = time();
        $new_filename = $filename_wo_type2 . $timestamp . $filename_type;
  
        if(move_uploaded_file($_FILES['image']['tmp_name'][$type], $upload_dir.$new_filename))
          $files[$type] = $new_filename;
      }else{
        echo "The $type file failed with code: {$_FILES['error'][$type]}<br />";
      }
    }
  }
  if(count($attachments)){
    echo " Files Uploaded Successfully<BR>";
    $sql = "insert into table (curriculum, portfolio, presentation_letter, other) values ('".$files['curriculum']."', '".$files['portfolio']."', '".$files['presentation_letter']."', '".$files['other']."');";
    print $sql;
    //You will have to put your SQL stuff here
//    SendIt(); //I commented this out cus I don't have this function
  } else {
    exit ("Sorry the server was unable to upload the files...");
  }

}

ok...more tweaking...try this now:

function uploadFile() {
  global $attachments;
  $upload_dir = "/home/emp1001/uploads/anuncio_completo/";
  chmod($upload_dir, 0775);
  
  $files = array();
  $allowed = array('curriculum','portfolio','presentation_letter','other');
  foreach (array_keys($_FILES['image']['name']) as $type ) {
    if($_FILES['image']['name'][$type] && in_array($type,$allowed)){
      if (empty ($_FILES['image']['error'][$type])) {
        $filename = $_FILES['image']['name'][$type];
        array_push($attachments, $filename);
          
        $original_filename = $filename;
        $filemane_lenght = strlen($original_filename); // see the lenght of the string
        $filename_wo_type = $filemane_lenght -4;
        $filename_wo_type2 = substr($original_filename, 0, $filename_wo_type);
        $filename_type = substr($filename, -4);
        $timestamp = time();
        $new_filename = $filename_wo_type2 . $timestamp . $filename_type;
  
        if(move_uploaded_file($_FILES['image']['tmp_name'][$type], $upload_dir.$new_filename))
          $files[$type] = $new_filename;
      }else{
        echo "The $type file failed with code: {$_FILES['error'][$type]}<br />";
      }
    }
  }
  if(count($attachments)){
    echo " Files Uploaded Successfully<BR>";
    $sql = "insert into table (curriculum, portfolio, presentation_letter, other) values ('".$files['curriculum']."', '".$files['portfolio']."', '".$files['presentation_letter']."', '".$files['other']."');";
    print $sql;
    //You will have to put your SQL stuff here
//    SendIt(); //I commented this out cus I don't have this function
  } else {
    exit ("Sorry the server was unable to upload the files...");
  }

}

 

Thanks a lot!! It worked now! Great!

 

Thanks again

 

Best Regards, André.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.