Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP] Pobieranie nazwy pliku upladowanego.
XP'ek
post
Post #1





Grupa: Zarejestrowani
Postów: 263
Pomógł: 9
Dołączył: 3.05.2009
Skąd: Wroc

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


Witam,

Więc tak mam taki skrypt. I chce by nazwa z upladowanego pliku była władowana do mysql'a

  1. <?
  2.  
  3. $host = "localhost";
  4. $user = "root";
  5. $pass = "";
  6. $name = "felis_foto";
  7.  
  8. $sql=mysql_connect($host, $user, $pass) or die('Brak połączenia z serwerem MySQL.Błąd: '.mysql_error());
  9. $db =mysql_select_db($name, $sql) or die('Nie mogę połączyć się z bazą danych.Błąd: '.mysql_error());
  10.  
  11. ?>
  12. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  13. <html xmlns="http://www.w3.org/1999/xhtml">
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  16. <title>Dokument bez tytułu</title>
  17. </head>
  18.  
  19. <body>
  20.  
  21. <?php
  22. if ($_POST['dodaj']) {
  23.  
  24. $id=$_GET['id'];
  25.  
  26. $newname1 = trim($_POST['newname']); // próbowałem też z nazwa 'image_name' i tak nie pobiera
  27.  
  28. //define a maxim size for the uploaded images in Kb
  29. define ("MAX_SIZE","100");
  30.  
  31. //This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
  32. function getExtension($str) {
  33. $i = strrpos($str,".");
  34. if (!$i) { return ""; }
  35. $l = strlen($str) - $i;
  36. $ext = substr($str,$i+1,$l);
  37. return $ext;
  38. }
  39.  
  40.  
  41. //reads the name of the file the user submitted for uploading
  42. $image=$_FILES['image']['name'];
  43. //if it is not empty
  44. if ($image)
  45. {
  46. //get the original name of the file from the clients machine
  47. $filename = stripslashes($_FILES['image']['name']);
  48. //get the extension of the file in a lower case format
  49. $extension = getExtension($filename);
  50. $extension = strtolower($extension);
  51. //if it is not a known extension, we will suppose it is an error and will not upload the file,
  52. //otherwise we will do more tests
  53. if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
  54. {
  55. //print error message
  56. echo '<h1>Unknown extension!</h1>';
  57. $errors=1;
  58. }
  59. else
  60. {
  61. //get the size of the image in bytes
  62. //$_FILES['image']['tmp_name'] is the temporary filename of the file
  63. //in which the uploaded file was stored on the server
  64. $size=filesize($_FILES['image']['tmp_name']);
  65.  
  66. //compare the size with the maxim size we defined and print error if bigger
  67. if ($size > MAX_SIZE*1024)
  68. {
  69. echo '<h1>You have exceeded the size limit!</h1>';
  70. $errors=1;
  71. }
  72.  
  73. //we will give an unique name, for example the time in unix time format
  74. $image_name=time().'.'.$extension;
  75. //the new name will be containing the full path where will be stored (images folder)
  76. $newname="tmp/".$image_name;
  77. //we verify if the image has been uploaded, and print error instead
  78. $copied = copy($_FILES['image']['tmp_name'], $newname);
  79. if (!$copied)
  80. {
  81. echo '<h1>Copy unsuccessfull!</h1>';
  82. $errors=1;
  83. }}}
  84.  
  85. //If no errors registred, print the success message
  86.  
  87.  
  88.  
  89.  
  90. // sprawdza czy taka podstrona jest w bazie
  91. $zapytanie = "SELECT nazwa FROM foto WHERE id='$id'";
  92. $offer = mysql_query($zapytanie);
  93.  
  94. if (mysql_num_rows($offer) > 0) print"";
  95.  
  96. else {
  97. $zapytanie = "INSERT INTO foto (id, foto) VALUES ('$id', '$newname1')";
  98. $wstaw = mysql_query($zapytanie);
  99.  
  100.  
  101.  
  102. if ($wstaw){
  103.  
  104. $zapytanie="SELECT * FROM `foto` WHERE `id` LIKE '$id'";
  105. $wykonaj=mysql_query($zapytanie);
  106.  
  107. if($wiersz=mysql_fetch_array($wykonaj))
  108. {
  109. $nazwa = $wiersz['nazwa'];
  110. $id = $wiersz['id'];
  111. }
  112.  
  113. echo "<div id='dymkaP'>Fotografia została właśnie dodana. Posiada identyfikator o numerze ".$id.". </div>";
  114. }
  115.  
  116. else
  117. echo "<div id='dymkaN'>Dodanie Fotografi do bazy nie powiodło się.</div>";
  118. }
  119. }
  120.  
  121. ?>
  122.  
  123. <div id="okno">
  124. <form enctype="multipart/form-data" action="index.php" method="POST" name="newad">
  125. <div class="oknoDane">
  126. <p class="oknoDanePotrzebne">Podaj Potrzebne Dane:</p>
  127. <table><tr>
  128. <td><table><tr>
  129. <td>fotografia:</td><td><input type="file" name="image" class="nazwy" /></td></tr></table></td>
  130. </tr></table>
  131. </div>
  132. <div class="pagesPrzyciski">
  133. <input type="submit" name="dodaj" class="pages" value="Dodaj" />
  134. <input type="reset" class="pages" value="Resetuj" />
  135. <input type="button" class="pages" onclick="java script:history.back();" name="wstecz" value="Wstecz" />
  136. </div>
  137. </form>
  138. </div>
  139. </body>
  140. </html>
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: 22.08.2025 - 19:06