0

我使用 foreach 根据用户名比较两个字符串,然后我只是从文件名中删除目标文件地址并将其存储在数据库中。当我手动输入实际文件名时,我可以获得输出。但是当我尝试在 foreach 中运行时,文件名仍然不受代码的影响。请有人帮忙...我已将有问题的代码加粗...

<?php

$directory = "shared/*/";

//get all image files with a .jpg extension.    
$images = glob("" . $directory . "*.jpg");
$imgs = '';

// create array
foreach($images as $image) { 
   $imgs[] = "$image"; 
}   

//shuffle array    
shuffle($imgs);     

//select first 20 images in randomized array    
$imgs = array_slice($imgs, 0, 20);    

//display images    
foreach ($imgs as $img) {    
    echo "<img width='320pts' hight='240pts' src='$img' /> ";   

    echo '<form method="post" >';
    echo '<input type="hidden" value="'.$img.'" name="cmt" />';
    echo '<input type="text" name="txt" />';
    echo "<input type='submit' value='comment' />";
    echo '</form>';    
}
?>    

<?php
   global $viky;
   $dbhost = 'localhost';
   $dbuser = 'viky';
   $dbpass = 'password';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);

   if(!$conn) {
       die('Could not connect: ' . mysql_error());
   }

   $sql = "SELECT username FROM register";
   mysql_select_db('login');
   $retval = mysql_query( $sql, $conn );

   if (!$retval) {
       die('Could not get data: ' . mysql_error());
   }

   while ($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {     
       $all[] = $row['username'];       
   }

   foreach ($all as $alls) { 
       $viky[] = $alls;
   }

   if (array_key_exists('cmt', $_POST)) {
       $filename = $_POST['cmt'];
       if (file_exists($filename)) {
        global $d;
        $d=$_POST['txt'];
            echo "{$_POST['txt']}";
      {
          {
          foreach($viky as $vig)
          {
            $str="{$filename}";
            $str2="shared/{$vig}/";
            echo $str2."<br>".$str."<br>";

            file_put_contents("tt.txt", "");
            $a=fopen('tt.txt','w');
            fwrite($a,$str );
        $haystack = file('tt.txt'); /* put contents into an array */
        $needle1 = "shared/{$vig}/"; /* value to be replaced */
        $needle2 =""; /* value doing the replacing */
        for($i=0;$i<count($haystack);$i++) { 
            $haystack[$i] = str_ireplace($needle1, $needle2, $haystack[$i]); /* case insensitive replacement */
        }
        $content = implode("\n", $haystack); /* put array into one variable with newline as delimiter */
        file_put_contents('tt.txt', $content); /* over write original with changes made */


        $needle1 = " "; /* value to be replaced */
        $needle2 ="_"; /* value doing the replacing */
        for($i=0;$i<count($haystack);$i++) { 
            $haystack[$i] = str_ireplace($needle1, $needle2, $haystack[$i]); /* case insensitive replacement */
        }
        $content = implode("\n", $haystack); /* put array into one variable with newline as delimiter */
        file_put_contents('tt.txt', $content); /* over write original with changes made */

        $needle1 = ".jpg"; /* value to be replaced */
        $needle2 =""; /* value doing the replacing */
        for($i=0;$i<count($haystack);$i++) { 
            $haystack[$i] = str_ireplace($needle1, $needle2, $haystack[$i]); /* case insensitive replacement */
        }
        $content = implode("\n", $haystack); /* put array into one variable with newline as delimiter */
        file_put_contents('tt.txt', $content); /* over write original with changes made */

        $c=file_get_contents("tt.txt");
        echo $c;


              }}
?>**






<?php

$dbhost = 'localhost';
$dbuser = 'viky';
$dbpass = 'password';
$db='comment';
$table=$c;

 $con=mysqli_connect($dbhost,$dbuser,$dbpass,$db);
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

 // Create table
$sql = "CREATE TABLE {$table} 
 (
 PID INT NOT NULL AUTO_INCREMENT, 
 PRIMARY KEY(PID),
 username CHAR(15),
 comment CHAR(15)
 )";

 // Execute query
 if (mysqli_query($con,$sql))
  {
  echo "Table persons created successfully";
  }
else
  {
  echo "Error creating table: " . mysqli_error($con);
  }
   mysqli_close($con);
 ?> 
 <?php


 $con=mysqli_connect($dbhost,$dbuser,$dbpass,$db);
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

mysqli_query($con,"INSERT INTO {$table} (username, comment)
 VALUES ('{$c}', '{$d}')");

 mysqli_close($con);



 ?>





 <?php

    }
        }


  else {
    echo 'Could not delete '.$filename.', file does not exist';
  }
  }

?>
4

1 回答 1

0

在你的代码中很难找到这个东西,但我可以说:

您必须使用引用才能使用 foreach 编辑数组中的当前元素。

喜欢

$names = array("A", "B") ;
foreach($names as &$name){ //Note sign & means reference
  $name = "AnotherName" ;
}
于 2013-06-20T18:02:20.797 回答