如何使用uploadify作为名称和返回时间值保存文件

时间:2014-12-11 01:51:14

标签: ajax uploadify

我打算做什么,是使用uploadify上传文件,在uploadify.php中更改其名称为time(),保存它,并将保存文件的名称返回给HTML,即时通讯不知道怎么做,目前我正在使用它作为指南

http://www.uploadify.com/documentation/uploadify/customizing-the-server-side-upload-script/

这是我目前的代码

的index.php

'<!DOCTYPE HTML>
 <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"      
 type="text/javascript"></script>
<script src="jquery.uploadify.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="uploadify.css">
<style type="text/css">
body {
font: 13px Arial, Helvetica, Sans-serif;
} 
</style>
</head>
<body>

<form>
    <div id="queue"></div>
    <input id="file_upload" name="file_upload" type="file" multiple="true">
</form>
<script type="text/javascript">
    <?php $timestamp = time();?>
    $(function() {
        $('#file_upload').uploadify({
            'formData'     : {
                'timestamp' : '<?php echo $timestamp;?>',
                'token'     : '<?php echo md5('unique_salt' . $timestamp);?>',
      'name'     : 'somename'  
            },
    'method' : 'post',
            'swf'      : 'uploadify.swf',
            'uploader' : 'uploadify.php',
             onUploadComplete : function(data, file , response){
  alert('The file was saved to: ' + data);
  alert(response);

            }           
        });
    }); 

 $(document).ready(function() {
 $("#FormSubmit").click(function (e) {
        var img = 'localhost/uploads/'+ $("#hidden").val;
        var cuenta_sap =  $("#cuenta_sap").val(); 
        var ubicacion =  $("#ubicacion").val(); 
        var codigo_centro_beneficio =  $("#codigo_centro_beneficio").val(); 
        var nombre =  $("#nombre").val(); 
        var proyecto = $ ("#proyecto").val();
        var departamento = $ ("#departamento").val();
        var tipo_activo = $("#tipo_activo").val();
        var proveedor = $("#proveedor").val();
        var modelo = $("#modelo").val();
        var numero_serie = $("#numero_serie").val();
        var marca = $ ("#marca").val();
        var numero_remision = $("#numero_remision").val();
        var myData ='cuenta_sap='+ cuenta_sap + '&ubicacion='+ ubicacion + 
       '&codigo_centro_beneficio='+ codigo_centro_beneficio + '&nombre=' + nombre + '&proyecto='    
        + proyecto + '&departamento='+ departamento + '&tipo_activo=' + tipo_activo 
      +'&proveedor='+ proveedor +'&modelo=' + modelo + '&numero_serie=' + numero_serie +'&marca='    
       + marca + '&numero_remision='+ numero_remision;

  e.preventDefault();


           if(cuenta_sap==''||ubicacion==''||codigo_centro_beneficio==''
           ||nombre==''||proyecto==''||departamento
            ==''||tipo_activo==''||proveedor==''||modelo==''
           ||numero_serie==''||marca==''||numero_remision=='')
  {
    alert("Formato incompleto!");
    return false;
  }

  $("#FormSubmit").hide(); //hide submit button
  $("#LoadingImage").show(); //show loading image

  $.ajax({
  type: "POST", // HTTP method POST or GET
  url: "response.php", //Where to make Ajax calls
  dataType:"text", // Data type, HTML, json etc.
  data:myData, //
  success:function(result){

     $("#responds").delay(10000).fadeIn();
    $("#responds").append("<li class='res'>"+result+"</li>");
          $('.res').remove();
          $("#proyecto").val(''); //empty text field on successful
          $("#departamento").val(''); //empty text field on successful
          $("#tipo_activo").val(''); //empty text field on successful
          $("#modelo").val(''); //empty text field on successful
          $("#numero_serie").val(''); //empty text field on successful
          $("#marca").val(''); //empty text field on successful
          $("#numero_remision").val(''); //empty text field on successful
    $("#cuenta_sap").val(''); //empty text field on successful
    $("#ubicacion").val(''); //empty text field on successful
    $("#nombre").val(''); //empty text field on successful
    $("#codigo_centro_beneficio").val(''); //empty text field on successful
    $("#FormSubmit").show(); //show submit button
    $("#LoadingImage").hide(); //hide loading image

  },
  error:function (xhr, ajaxOptions, thrownError){
    $("#FormSubmit").show(); //show submit button
    $("#LoadingImage").hide(); //hide loading image
    alert(thrownError);
  }
  });
  });

$("body").on("click", "#responds .del_button", function(e) {
 e.preventDefault();
 var clickedID = this.id.split('-'); //Split ID string (Split works as PHP explode)
 var DbNumberID = clickedID[1]; //and get number from array
 var myData = 'recordToDelete='+ DbNumberID; //build a post data structure

 $('#item_'+DbNumberID).addClass( "sel" ); //change background of this element by adding class
 $(this).hide(); //hide currently clicked delete button

  $.ajax({
  type: "POST", // HTTP method POST or GET
  url: "response.php", //Where to make Ajax calls
  dataType:"text", // Data type, HTML, json etc.
  data:myData, //Form variables
  success:function(response){
    //on success, hide  element user wants to delete.
    $('#item_'+DbNumberID).fadeOut();
  },
  error:function (xhr, ajaxOptions, thrownError){
    //On error, we alert user
    alert(thrownError);
  }
  });
});

});
</script>
<div class="content_wrapper" id="wrapepr">
<div class="form_style">
<input name="hidden" id="hidden" type="text"></input>
<br>
<a>Datos</a>
<br>
<input name="cuenta_sap" id="cuenta_sap" placeholder="cuenta sap" class="input"></input>

<select name="departamento" class="input" id="departamento">
<option value = "">seleccione departamento</option>
<?php
include("db.php");
$stmt = $db->prepare("SELECT `id`, `departamento` FROM `departamentos`");
$stmt->execute();
$stmt->bind_result($id,$departamento);
while ($stmt->fetch()){
echo "<option value='$id'>$departamento</option>";
}
?>
  </select>  
  <select name="proyecto" class="input" id="proyecto" >
<option value = "" >seleccione proyecto</option>
<?php

$stmt = $db->prepare("SELECT `id` ,`proyecto` FROM `proyectos`");
$stmt->execute();
$stmt->bind_result($id,$proyecto);
while ($stmt->fetch()){
echo "<option value='$id'>$proyecto</option>";
}
?>
  </select>  
  <select name="tipo" class="input" id="tipo_activo">
<option value = "" class="input" >tipo de activo</option>
<?php

$stmt = $db->prepare("SELECT `id` ,`tipo` FROM `tipo_activo`");
$stmt->execute();
$stmt->bind_result($id,$tipo_activo);
while ($stmt->fetch()){
echo "<option value='$id' class='input'>$tipo_activo</option>";
}
?>
  </select>  
    <select name="proveedor" class="input" id="proveedor">
<option value = "" class="input" >seleccione proveedor</option>
<?php

$stmt = $db->prepare("SELECT `id` ,`nombre` FROM `proveedores`");
 $stmt->execute();
 $stmt->bind_result($id,$nombre);
while ($stmt->fetch()){
echo "<option value='$id' class='input'>$nombre</option>";
}
?>
  </select>  
<input name="codigo_centro_beneficio" id="codigo_centro_beneficio"  placeholder="codigo centro      
  beneficio" class="input"></input>

<input name="nombre" id="nombre"  placeholder="nombre"  class="input"></input>

 <input name="modelo" id="modelo"  placeholder="modelo"  class="input"></input>

  <input name="numero_serie" id="numero_serie"  placeholder="numero_serie"  class="input">    
 </input>

   <input name="marca" id="marca"  placeholder="marca"  class="input"></input>

<input name="numero_remision" id="numero_remision" placeholder="numero remision" class="input">       
</input>

<input name="upload" id="upload" class="input" type="file"></input>
<button id="FormSubmit">registrar</button>

<img src="images/loading.gif" id="LoadingImage" style="display:none" />
</div>
<ul id="responds" >

</ul> 
</div>

</body>
</html>'

和uploadify.php    &#39;

$targetFolder = '/uploads'; // Relative to the root

$verifyToken = md5('unique_salt' . $_POST['timestamp']);

if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
$now = time();
$name = $_POST('name')FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;


// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);
$targetFile = rtrim($targetPath,'/') . '/' . $now .'.'. $fileParts['extension'];
echo rtrim($targetPath,'/') . '/' . $now .'.'. $fileParts['extension'];
if(file_exists($targetFile)){
    ++$now;
    $targetFile = rtrim($targetPath,'/') . '/' . $now .'.'. $fileParts['extension'];
      $name $targetPath,'/') . '/' . $now .'.'. $fileParts['extension'];
      echo $name;

      }


if (in_array($fileParts['extension'],$fileTypes)) {

  move_uploaded_file($tempFile,$targetFile);

} 
else {
    echo 'Invalid file type.';


}
?>'

1 个答案:

答案 0 :(得分:0)

我找到了答案,经过一段时间的搜索和阅读uploadify手册后,如果你使用onUploadComplete,你只需上传一个单一的罚款,该方法将永远不会超出,它将继续等待更多的文件,而只是一个文件,你必须使用onUploadSuccess,并使用文件,数据,响应作为参数读取它返回的数据。