Jump to content

Recommended Posts

Hi I am working on a PHP script to upload files to the server and write the names in a mysql dbase. The upload script always works and when using a single file upload I get it to write the file name into the dbase. With multiple uploads I have been reading the manual and can't work out how to split the array values as  want to then allocated them to variable names to then insert into individual columns in the dbase.

If I try :

[code]
<?php
foreach($_FILES as $file_name => $file_array) {
    echo $file_array['name']."<br>\n";
}

?>
[/code]

It prints out the whole list of file names stored in the array and if I try the following:

[code]
<?php
foreach($_FILES as $file_name => $file_array) {
    echo $file_array['name']."<br>\n";
}
    echo $file_array['name'][2]."<br>\n";

?>
[/code]

It will print out the list of file names held in the $file_array['name'] array and then the corresponding letter in the last file name uploaded which is held in the array. Iin this case the third one.

As such want I need is to split the arrays and then give then variable names such as the following:

$var1 = the first uploaded file name from the
$var2 = the second uploaded file name etc etc.

Any help would be appreciated. Full script below:

regards


[code]

<html>
<?php require_once('../../Connections/Punks_conn.php'); ?>

<head>
<title>form variable insert</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
$name = $_POST[Firstname];
$surname =$_POST[Surname];
$group = $_POST[Group];

//folder creation using chmod change in permissions to 777
$file_dir = "../test7/uploads/";
if( !file_exists( $file_dir ) )
{
  mkdir( $file_dir );
  chmod( $file_dir, 0777 );
                         
}
?>



<?php
foreach($_FILES as $file_name => $file_array) {
    echo "path: ".$file_array['tmp_name']."<br>\n";
    echo "name: ".$file_array['name']."<br>\n";
    echo "type: ".$file_array['type']."<br>\n";
    echo "size: ".$file_array['size']."<br>\n";

    if (is_uploaded_file($file_array['tmp_name'])) {
        move_uploaded_file($file_array['tmp_name'], "$file_dir/$file_array[name]") or die ("Couldn't copy");
        echo "file was uploaded!<br><br>";
    }
}


?>

<?php
/* This prints out a list of the uploaded file names but I need to separate the names and give them variable names*/
foreach($_FILES as $file_name => $file_array) {
    echo $file_array['name']."<br>\n";
}
    echo $file_array['name'][2]."<br>\n";

?>



<?php // upload the form details

mysql_select_db($database_Punks_conn, $Punks_conn);
$sql = "INSERT INTO punks (FirstName, Surname, `Group`, Photo1, Photo2,Photo3,Photo4 ) VALUES ('$name','$surname','$group', '$photo1', '$photo2','$photo3','$photo4')";
//execute the SQL Statement
if (mysql_query($sql, $Punks_conn)) {
echo "new listing created";
}
else {
echo "This one never made it";
}
//error identifier
?>

</body>
</html>

[/code]
Link to comment
https://forums.phpfreaks.com/topic/13296-splitting-array-results/
Share on other sites

First, I would change your database structure so there aren't multiple images in a row.  I think that it would be better form to just enter each photo in as its own row and have a relational column to connect them.

If you do what I just suggested then you can use a foreach loop to insert the photos on at a time...
[code]
$query = ''; // create the string
foreach ($photos as $photo)
{
$query .= "INSERT INTO $table ... ;\n"; // add a new query to the string, remember the ";"
}
$result = @mysql_query... ; // ok so you just entered them all in now check for errors
[/code]
HI thanks for your help but I don't think I can use it I need to keep the database structure as it is and so it doesn't really solve the problem. Once again it is merely a question of taking out the values as are displayed with this code.......

[code]

<?php
foreach($_FILES as $file_name => $file_array) {
    echo $file_array['name']."<br>\n";
}

?>

When I have tried to print out separate parts of the array such as the second (out of a possible four) it will just print out a letter of the name of the last file uploaded. (This is explained in the earlier posted details.)

So what i would like to do is split the array and allocate them to variables or any suggestion that allows me to input each file name which has been uploaded in a column in the mysql database. eg. $Var1 into photo1 , $var2 into photo2 etc.

Hope someone can help! 


[/code]
See if this helps:

[code]<?php require_once('../../Connections/Punks_conn.php'); ?>
<html>
<head>
<title>form variable insert</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
$name = $_POST['Firstname'];
$surname = $_POST['Surname'];
$group = $_POST['Group'];

//folder creation using chmod change in permissions to 777
$file_dir = "../test7/uploads/";
if (!file_exists($file_dir)) {
mkdir( $file_dir );
chmod( $file_dir, 0777 );
}

$i = 0;
foreach ($_FILES as $file_name => $file_array) {
    echo "path: ".$file_array['tmp_name']."<br>\n";
    echo "name: ".$file_array['name']."<br>\n";
    echo "type: ".$file_array['type']."<br>\n";
    echo "size: ".$file_array['size']."<br>\n";

    if (is_uploaded_file($file_array['tmp_name'])) {
        move_uploaded_file($file_array['tmp_name'], "$file_dir/$file_array[name]") or echo ("Couldn't copy!!<br><br>");
        echo "file was uploaded!<br><br>";
    }
$photo{$i} = $file_array['name'];

}

mysql_select_db($database_Punks_conn, $Punks_conn);
$sql = "INSERT INTO punks (FirstName, Surname, `Group`, Photo1, Photo2,Photo3,Photo4 ) VALUES ('$name','$surname','$group', '$photo1', '$photo2','$photo3','$photo4')";
//execute the SQL Statement
if (mysql_query($sql, $Punks_conn)) {
echo "new listing created";
} else {
echo "This one never made it";
}
?>
</body>
</html>[/code]
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.