开发者

uploadify - rename uploaded file

Hy , i was looking at uploadify.php and did'n understand a thing.

I have a form like this :

<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>

and my uploadify settings are :

<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>

What i want to do is that if the user select Apples wich has the id=1 and browse for a file like Tasty_apples.jpg -> the uploaded file to be renames to product@1@Tasty_apples.jpg and then to be inserted in mysql like that?

The main question is how to add the extra product@id@ to a file based on a <select&开发者_StackOverflow中文版gt;<option> value ?

Thank you very much

The uploadify.php is this :

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);
}


I think i have solved this...try this in your upload.php file

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);

}


You can send additional data to your backend script with scriptData option:

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

Example

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

'scriptData'  : {'pid': selectedID}

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


it would be good if you could provide your actual 'uploadify.php' file, to help with the actual PHP. But as an example of how you would go about changing the name it would be something along the lines of this:

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

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

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

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

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

This is just an example, if I had your code I could point it out better; but hope that's understandable!


//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); 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜