uploadify - 重命名上传的文件

时间:2011-09-09 14:37:49

标签: php uploadify

Hy,我正在看uploadify.php,并且对一件事情一无所知。

我有一个这样的表格:

<form id="formid" name="upload_pic" action="upload.php">
<select name="product_id">
<option value="1">Apples</option>
<option value="2">Oranges</option>
... etc
</select>
<input id="file_upload" name="file_upload" />
</form>

我的uploadify设置为:

<script type="text/javascript">
$(document).ready(function() {
  $('#file_upload').uploadify({
    'uploader'  : 'uploadify/uploadify.swf',
    'script'    : 'uploadify/uploadify.php',
    'cancelImg' : 'uploadify/cancel.png',
    'folder'    : '../images/level3/tabv_all/tab_header/',
    'auto'      : false,
    'multi'     : true,
    'fileExt'     : '*.jpg',
    'fileDesc'    : 'ONLY JPG (.JPG)',
    'removeCompleted' : false
  });
});
</script>

我想要做的是,如果用户选择Applesid=1browse for a file like Tasty_apples.jpg - &gt;将上传的文件重命名为product@1@Tasty_apples.jpg,然后将其插入到mysql中?

主要问题是如何根据product@id@将额外的<select><option> value添加到文件中?

非常感谢

uploadify.php是这样的:

if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

    move_uploaded_file($tempFile,$targetFile);
    echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
}

4 个答案:

答案 0 :(得分:2)

我想我已经解决了这个问题...在你的upload.php文件中试试这个

if (!empty($_FILES)) {

$tempFile = $_FILES['Filedata']['tmp_name'];
$newName = $_FILES['Filedata']['name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'];
if(file_exists($targetPath."/".$newName))
         {
         //echo "test";exit;
            $part=explode("." , $newName);
            $name1=$part[0];
            $ext=$part[1];
            $newName=$name1."_".rand().".".$ext;
        }
$Path = $targetPath . '/';
$targetFile =  str_replace('//','/',$Path) . $newName;
move_uploaded_file($tempFile,$targetFile);

}

答案 1 :(得分:1)

您可以使用scriptData选项将附加数据发送到后端脚本:

http://www.uploadify.com/documentation/options/scriptdata/

示例

var selectedID = $("select[name=product_id]").val()

'scriptData'  : {'pid': selectedID}

// uplodify.php
$targetFile =  str_replace('//','/',$targetPath) . 'product@' . $_POST['pid'] . '@' . $_FILES['Filedata']['name'];

答案 2 :(得分:1)

如果您能提供实际的uploadify.php&#39;那将是件好事。文件,以帮助实际的PHP。但作为一个如何改变名称的例子,它将是这样的:

$tmp_name = @$_FILES['Filedata']['tmp_name'];

$name = @$_FILES['Filedata']['name'];

$filesize = @$_FILES['Filedata']['size'];

$extension  = strtolower(pathinfo($name,PATHINFO_EXTENSION));

$newname = 'apples&'.$name . "." . $extension ;

这只是一个例子,如果我有你的代码,我可以更好地指出它;但希望这是可以理解的!

答案 3 :(得分:1)

//your categories array, example
$cats = array(1=>'apples',2=>'oranges');

if (!empty($_FILES)) {
  $tempFile = $_FILES['Filedata']['tmp_name'];
  $targetPath = $_SERVER['DOCUMENT_ROOT'] .'/'. trim($_REQUEST['folder'], '/') . '/';
  $name = pathinfo($_FILES['Filedata']['name'], PATHINFO_FILENAME);
  $extension = strtolower(pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION));
  $newname = (isset($cats[$_REQUEST["product_id"]]) ? $cats[$_REQUEST["product_id"]] : 'category_not_exist' ).'@'. (int)$_REQUEST["product_id"].'@'. $name '.' . $extension;
  $targetFile = str_replace('//','/',$targetPath) . $newname;
  move_uploaded_file($tempFile, $targetFile);
  echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile); 
}