Board logo

標題: Upload Multiple File PHP code [打印本頁]

作者: CSA41200    時間: 2013-10-31 11:12     標題: Upload Multiple File PHP code

本帖最後由 CSA41200 於 2013-10-31 11:14 編輯

各位ching,請問可以點改個code,係每次refresh之後,唔會再出現"XXX uploaded successfully"

code 如下:

<?php
if($_POST['pgaction']=="upload")
        upload();
else
        uploadForm();

//The form having dynamic file uploader
function uploadForm() {
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title> :: FILEUPLOAD :: </title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body bgcolor="#C8C8C8" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">
<br>
<form name="frm" method="post" onsubmit="return validate(this);" enctype="multipart/form-data">
<input type="hidden" name="pgaction">
        <?php if ($GLOBALS['msg']) { echo '<center><span class="err">'.$GLOBALS['msg'].'</span></center>'; }?>
        <table align="center" cellpadding="4" cellspacing="0" bgcolor="#EDEDED">       
                <tr class="tblSubHead">
                        <td colspan="2">Upload any number of file</td>
                </tr>
                <tr class="txt">
                        <td valign="top"><div id="dvFile"><input type="file" name="item_file[]"></div></td>
                        <td valign="top"><a href="javascript:_add_more();" title="Add more"><img src="plus_icon.gif" border="0"></a></td>
                </tr>
                <tr>
                        <td align="center" colspan="2"><input type="submit" value="Upload File"></td>
                </tr>
        </table>
</form>
<script language="javascript">
<!--
        function _add_more() {
                var txt = "<br><input type=\"file\" name=\"item_file[]\">";
                document.getElementById("dvFile").innerHTML += txt;
        }
        function validate(f){
                var chkFlg = false;
                for(var i=0; i < f.length; i++) {
                        if(f.elements.type=="file" && f.elements.value != "") {
                                chkFlg = true;
                        }
                }
                if(!chkFlg) {
                        alert('Please browse/choose at least one file');
                        return false;
                }
                f.pgaction.value='upload';
                return true;
        }
//-->
</script>
</body>
</html>
<?php
}

//function to store uploaded file

function upload(){       
        if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded
                $GLOBALS['msg'] = ""; //initiate the global message
                for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array
                        $filen = $_FILES["item_file"]['name']["$j"]; //file name
                        $path = 'webupload/'.$filen; //generate the destination path
                        if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file
                                $GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>"; //Success message
                        }
                }
        }
        else {
                $GLOBALS['msg'] = "No files found to upload"; //Failed message       
        }
        uploadForm(); //display the main form
}
?>
作者: educationer    時間: 2013-10-31 11:32

你show完msg 唔清番佢 咁你upload完一次之後佢永遠都係度 咪每次都會彈success message 最簡單試試
<?php if ($GLOBALS['msg']) { echo '<center><span class="err">'.$GLOBALS['msg'].'</span></center>';  unset($GLOBALS['msg']);}?>
作者: CSA41200    時間: 2013-11-1 01:39

你show完msg 唔清番佢 咁你upload完一次之後佢永遠都係度 咪每次都會彈success message 最簡單試試
  ...
educationer 發表於 2013-10-31 11:32


ching,跟你做法都係無效 >.<
有無其他方法?
作者: CSA41200    時間: 2013-11-1 02:20

help~~~~~
作者: educationer    時間: 2013-11-1 11:32

本帖最後由 educationer 於 2013-11-1 12:03 編輯

可能係if statment condition 問題,因(而家$GLOBALS['msg']係true就會show msg?) 而唔係check佢係咪空既,

一係就

<?php if (isset($GLOBALS['msg'])) { echo '<center><span class="err">'.$GLOBALS['msg'].'</span></center>';  unset($GLOBALS['msg']);}?>

一係就

<?php if ($GLOBALS['msg'] != "") { echo '<center><span class="err">'.$GLOBALS['msg'].'</span></center>';  $GLOBALS['msg'] = "";}?>
作者: CSA41200    時間: 2013-11-2 00:18

本帖最後由 CSA41200 於 2013-11-2 00:21 編輯
可能係if statment condition 問題,因(而家$GLOBALS['msg']係true就會show msg?) 而唔係check佢係咪空既, ...
educationer 發表於 2013-11-1 11:32


educationer ching,跟你ge做法都係唔得 >.<
1. 起初係咁ge
[attach]1600390[/attach]
2. 成功upload係咁ge
[attach]1600391[/attach]
3. 按F5 refresh
[attach]1600394[/attach]
4. 之後就變成咁
[attach]1600395[/attach]

已經無野upload,按F5 refresh,竟然"XXX 成功upload"句野仲係返度  >.<
作者: CSA41200    時間: 2013-11-3 00:29

各位ching,個php檔已經可以係下面條link dl,可唔可以幫我睇下邊度有問題,引致我上面所講ge情況出現!!!
thanks !!!
請下載
作者: justlazy    時間: 2013-11-3 14:12

回復 6# CSA41200

你撳重試果陣等於resubmit左張form,所以有message係正常。
作者: CSA41200    時間: 2013-11-3 15:47

回復  CSA41200

你撳重試果陣等於resubmit左張form,所以有message係正常。
justlazy 發表於 2013-11-3 14:12



   
ching,咁可以點搞?
作者: max918    時間: 2013-11-3 17:25

refresh 就自然會將個post做多次, 呢件係好合理既事

如果真係要避, 可以格硬係upload success 做個 header redirect 之類等browser refresh唔再submit
作者: tonyli1212    時間: 2013-11-3 17:56

if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file
      $GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>"; //Success message
      header('Location: ?'.rand()); //加呢行
}

作者: CSA41200    時間: 2013-11-4 21:26

本帖最後由 CSA41200 於 2013-11-4 21:48 編輯
if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file
      $GLOB ...
tonyli1212 發表於 2013-11-3 17:56


ching,跟你做法係得
但係無左upload成功果個message
有無方法show返果句成功upload ge message?

anyway,thanks !!!
作者: tonyli1212    時間: 2013-11-4 23:11

try this
  1. function upload(){       
  2.         if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded
  3.                 $GLOBALS['msg'] = ""; //initiate the global message
  4.                 for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array
  5.                         $filen = $_FILES["item_file"]['name']["$j"]; //file name
  6.                         $path = 'webupload/'.$filen; //generate the destination path
  7.                         if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file
  8.                                 $GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>"; //Success message
  9.                         }
  10.                 }
  11.         }
  12.         else {
  13.                 $GLOBALS['msg'] = "No files found to upload"; //Failed message       
  14.         }
  15.         //uploadForm(); //display the main form
  16.       header('Location: ?msg='.urlencode($GLOBALS['msg']));
  17. }
複製代碼
and in the uploadForm(), change
  1.         <?php if ($GLOBALS['msg']) { echo '<center><span class="err">'.$GLOBALS['msg'].'</span></center>'; }?>
複製代碼
to
  1.         <?php if ($_GET['msg']) { echo '<center><span class="err">'.$_GET['msg'].'</span></center>'; }?>
複製代碼

作者: CSA41200    時間: 2013-11-5 20:33

try thisand in the uploadForm(), changeto
tonyli1212 發表於 2013-11-4 23:11


ching,跟你ge做法的確係work,但係就無左upload成功ge message
即係圖中紅色果句
[attach]1601589[/attach]
以下就係我成個php upload package下載link,希望各位高手幫我睇一睇
http://www.sendspace.com/file/zh69yc
作者: justlazy    時間: 2013-11-5 23:03

如果你 submit 過張 form,refresh browser 撳確定會叫個 browser 將你頭先尐野重新 submit 一次,所以有 successful message,係無錯架喎...
作者: CSA41200    時間: 2013-11-5 23:30

如果你 submit 過張 form,refresh browser 撳確定會叫個 browser 將你頭先尐野重新 submit 一次,所以有 s ...
justlazy 發表於 2013-11-5 23:03


ching,有無方法做到兩全其美??
因小弟的php知識是有限公司...
作者: justlazy    時間: 2013-11-6 01:39

回復 16# CSA41200

呢個係browser behaviour,唔關PHP事。如果你真係好唔想人地submit完refresh會重新submit,可以做個server side redirect去返同一頁.
作者: CSA41200    時間: 2013-11-6 06:34

回復  CSA41200

呢個係browser behaviour,唔關PHP事。如果你真係好唔想人地submit完refresh會重新submit ...
justlazy 發表於 2013-11-6 01:39


server side係點搞?
作者: CSA41200    時間: 2013-11-6 23:35

help~~~~
作者: szejie    時間: 2013-11-6 23:51

server side係點搞?
CSA41200 發表於 2013-11-6 06:34


試下submit完用header redirect啦
一係加個session黎check住佢
作者: justlazy    時間: 2013-11-7 02:53

  1. <?php header('Location: <url>'); ?>
複製代碼





歡迎光臨 電腦領域 HKEPC Hardware (https://h0.hkepc.com/forum/) Powered by Discuz! 7.2