PHP使用foreach函数创建数组

时间:2013-08-10 14:25:46

标签: php foreach

我尝试将文件路径放入邮件中以便在没有ftp的情况下下载

$nomecognome    = $_POST['cname'];
$email          = $_POST['cemail'];
$biography      = $_POST['ctext'];
$datanascita    = $_POST['cdata'];
$controllo      = $_POST['ccontrol'];
$files          = $_FILES['uploader'];

if(empty($controllo)){

我清除了名字

$accenti = array( 'à' , 'è' , 'é' , 'ì' , 'ò' , 'ù' , '\'' , ' ' );
$noaccenti = array( 'a' , 'e' , 'e' , 'i' , 'o' , 'u' , '_' , '_' );
$author = strtolower( str_replace( $accenti , $noaccenti , $nomecognome ) );

为将来的比较文件ext

创建一个数组
$allowedExtensions = array('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif', 'pdf');

控制表单是否为空

if ( !empty ( $files ) ){

开始上传图片

    foreach ($files['name'] as $key => $value) {

        if(is_uploaded_file($files['tmp_name'][$key]) && $files['error'][$key] == 0) {

使用用户名和随机数加文件名

为文件创建唯一名称
            $filename = $files['name'][$key];
            $filename = $author.rand(0,99).$filename;
            //array_push($path, 'http://www.magic-promo.it/uploads/'.$author.$value);

检查文件是否已移动

            if( move_uploaded_file( $files['tmp_name'][$key], 'uploads/'. $filename) ) {

这里我想创建每个图像路径的数组以发送邮件

                foreach ($filename as $filenames) {
                    $pathfile[] = array($filename);
                }

                $body = implode(',', $pathfile);
                echo $body; //to view the list of path
            }
            else{
                echo move_uploaded_file($files['tmp_name'][$key], 'uploads/'. $filename);
                echo 'file non uploadato';
            }
        }
        else {
            echo 'The file was not uploaded.';
        }

    }

我 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

试试这个:

$pathfile = array(); // prevent undefined variable notice
if ( !empty ( $files ) ) {
    foreach ($files['name'] as $key => $value) {
        if(is_uploaded_file($files['tmp_name'][$key]) && $files['error'][$key] == 0) {

            // Create an unique name for the file using the name of user and random number plus filename
            $filename = $files['name'][$key];
            $filename = $author.rand(0,99).$filename;
            //array_push($path, 'http://www.magic-promo.it/uploads/'.$author.$value);

            //Check if the file was moved
            if( move_uploaded_file( $files['tmp_name'][$key], 'uploads/'. $filename) ) {
                $pathfile[] = $filename;                
            } else {
                echo move_uploaded_file($files['tmp_name'][$key], 'uploads/'. $filename);
                echo 'file non uploadato';
            }
        } else {
            echo 'The file was not uploaded.';
        }
    }
}
$body = implode(',', $pathfile);        // $body = implode("\n", $pathfile);
echo $body; //to view the list of path