Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP] Pobieranie nazwy pliku upladowanego.
Forum PHP.pl > Forum > Przedszkole
XP'ek
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>
hondek
  1. $newname1 = $_FILES['image']['name'];
XP'ek
zamieniłem to
  1. $newname1 = trim($_POST['newname']);
na
  1. $image_name = $_GET['image_name'];
i teraz działa tak jak chciałem wysyła mi nazwa.jpg smile.gif ale teraz jeszcze przydało by się zrobić pomniejszanie zdjęcia do rozmiarów 800x600 jaką funkcje polecacie ?

ps. kolego @hondek twoje zapytanie pobiera nazwę upladowanego pliku smile.gif a podczas upka nazwa pliku jest zmieniana smile.gif
hondek
@XP'ek

Ta zmienna przechoduje nazwę uploadowanego zdjęcia i z tego co wiem nie jest zmieniana....
Natomiast skąd u ciebie sie bierze zmienna $_GET['image_name'] ? Nic getem tutaj nikt nie wysyła...
XP'ek
to masz sprawdź sobie smile.gif
tylko w tym pliku co będzie index zrób tmp folder i zobaczysz działa smile.gif

  1. --
  2. -- Struktura tabeli dla `foto`
  3. --
  4.  
  5. CREATE TABLE `foto` (
  6. `id` int(11) NOT NULL AUTO_INCREMENT,
  7. `foto` varchar(255) collate utf8_polish_ci NOT NULL,
  8. PRIMARY KEY (`id`)
  9. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci AUTO_INCREMENT=20 ;
  10.  
  11. --
  12. -- Zrzut danych tabeli `foto`
  13. --
  14.  
  15. INSERT INTO `foto` (`id`, `foto`) VALUES
  16. (19, '1280653017.jpg'),
  17. (18, 'DSC00663.JPG'),
  18. (17, '1280652768.jpg'),
  19. (16, 'tmp/1280652708.jpg'),
  20. (15, 'tmp/1280652658.jpg');


i plik index.php
  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. $image_name = $_GET['image_name']; // 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 = "INSERT INTO foto (id, foto) VALUES ('$id', '$image_name')";
  92. $wstaw = mysql_query($zapytanie);
  93.  
  94.  
  95.  
  96. if ($wstaw){
  97. echo "<div id='dymkaP'>Fotografia została właśnie dodana. Posiada identyfikator o numerze ".$id.". </div>";
  98. }
  99.  
  100. else
  101. echo "<div id='dymkaN'>Dodanie Fotografi do bazy nie powiodło się.</div>";
  102. }
  103.  
  104.  
  105. ?>
  106.  
  107. <div id="okno">
  108. <form enctype="multipart/form-data" action="index.php" method="POST" name="newad">
  109. <div class="oknoDane">
  110. <p class="oknoDanePotrzebne">Podaj Potrzebne Dane:</p>
  111. <table><tr>
  112. <td><table><tr>
  113. <td>fotografia:</td><td><input type="file" name="image" class="nazwy" /></td></tr></table></td>
  114. </tr></table>
  115. </div>
  116. <div class="pagesPrzyciski">
  117. <input type="submit" name="dodaj" class="pages" value="Dodaj" />
  118. <input type="reset" class="pages" value="Resetuj" />
  119. <input type="button" class="pages" onclick="java script:history.back();" name="wstecz" value="Wstecz" />
  120. </div>
  121. </form>
  122. </div>
  123. </body>
  124. </html>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.