Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JavaScript][PHP]Uploader plików - błąd w warunku
sadistic_son
post
Post #1





Grupa: Zarejestrowani
Postów: 1 495
Pomógł: 245
Dołączył: 1.07.2009
Skąd: Bydgoszcz

Ostrzeżenie: (0%)
-----


Cześć,
Zainteresowałem się prostym uploaderem plików stąd. Miałby on wgrywać pliki bez przeładowywania strony. I tutaj pierwsze pytanie - czy można bezkarnie używać pływających ramek - iframe? Pamiętam jak uczyłem się php na początku obecnego wieku, to już wtedy było w książce, żeby raczej odchodzić o używania iframe. Jak to jest obecnie?


Ale do rzeczy. W JavaScript ogólnie raczkuję. Ni epotrzafię znaleźć pczyczyny dlaczego nie wykonuje się warunek if (success == 1){ , czyli jest komunikat There was an error during file upload!.


index.html:
  1. <script type="text/javascript">
  2. function startUpload(){
  3. document.getElementById('uploadProcess').style.visibility = 'visible';
  4. document.getElementById('uploadForm').style.visibility = 'hidden';
  5. return true;
  6. }
  7.  
  8. function stopUpload(success,uploadedFile){
  9. var result = '';
  10. if (success == 1){
  11. result = '<span class="sucess-msg">The file was uploaded successfully!<\/span><br/><br/>';
  12. //Uploaded file preview
  13. var embed = document.getElementById("UploadedFile");
  14. var clone = embed.cloneNode(true);
  15. clone.setAttribute('src',uploadedFile);
  16. embed.parentNode.replaceChild(clone,embed);
  17. }else {
  18. result = '<span class="error-msg">There was an error during file upload!<\/span><br/><br/>';
  19. }
  20. document.getElementById('uploadProcess').style.visibility = 'hidden';
  21. document.getElementById('uploadForm').innerHTML = result + '<label>File: <input name="myfile" type="file" size="30" /><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>';
  22. document.getElementById('uploadForm').style.visibility = 'visible';
  23. return true;
  24. }
  25.  
  26. </head>
  27. <form action="upload.php" method="post" enctype="multipart/form-data" target="uploadTarget" onsubmit="startUpload();" >
  28. <p id="uploadProcess">Uploading...<br/><img src="assets/loader.gif" /><br/></p>
  29. <p id="uploadForm" align="center"><br/>
  30. File: <input name="myfile" type="file" size="30" />
  31. </label>
  32. <input type="submit" name="submitBtn" class="sbtn" value="Upload" />
  33. </label>
  34. </p>
  35. <iframe id="uploadTarget" name="uploadTarget" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
  36. </form>
  37. <!-- Uploaded file preview -->
  38. <div>
  39. <embed id="UploadedFile" src="" width="390px" height="160px">
  40. </div>
  41. </body>
  42. </html>


upload.php:
  1. <?php
  2. $success = 0;
  3. $uploadedFile = '';
  4.  
  5. //File upload path
  6. $uploadPath = 'uploads/';
  7. $targetPath = $uploadPath . basename( $_FILES['myfile']['name']);
  8.  
  9. if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $targetPath)){
  10. $success = 1;
  11. $uploadedFile = $targetPath;
  12. }
  13.  
  14. sleep(1);
  15. ?>
  16. <script type="text/javascript">window.top.window.stopUpload(<?php echo $success; ?>,'<?php echo $uploadedFile; ?>');</script>

Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 27.09.2025 - 15:26