473,657 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

the addalbum.php is unable to upload the image any idea what i'm doing wrong please

29 New Member
Hi i have this form which meant to upload the create an album but some how doesnt not work can some1 help please i use 3 files here they are addalbum.php is where is not working the other files are just the database config and functions.php the error messages from the addalbum.php functions


addalbum.php
Expand|Select|Wrap|Line Numbers
  1.  require_once '../library/config.php';
  2. require_once '../library/functions.php';
  3.  
  4. if(isset($_POST['txtName']))
  5. {
  6.    $albumName = $_POST['txtName'];
  7.    $albumDesc = $_POST['mtxDesc'];
  8.  
  9.    $imgName = $_FILES['fleImage']['name'];
  10.    $tmpName = $_FILES['fleImage']['tmp_name'];
  11.  
  12.    // we need to rename the image name just to avoid
  13.    // duplicate file names
  14.    // first get the file extension
  15.    $ext = strrchr($imgName, ".");
  16.  
  17.    // then create a new random name
  18.    $newName = md5(rand() * time()) . $ext;
  19.  
  20.    // the album image will be saved here
  21.    $imgPath = ALBUM_IMG_DIR . $newName;
  22.  
  23.    // resize all album image
  24.    $result = createThumbnail($tmpName, $imgPath, THUMBNAIL_WIDTH);
  25.  
  26.    if (!$result) {
  27.       echo "Error uploading file";
  28.       exit;
  29.    }
  30.  
  31.    if (!get_magic_quotes_gpc()) {
  32.       $albumName = addslashes($albumName);
  33.       $albumDesc = addslashes($albumDesc);
  34.    }
  35.  
  36.    $query = "INSERT INTO tbl_album (al_name, al_description, al_image, al_date)
  37.    VALUES ('$albumName', '$albumDesc', '$newName', NOW())";
  38.  
  39.    mysql_query($query)
  40.    or die('Error, add album failed : ' .    mysql_error());
  41.  
  42.    // the album is saved, go to the album list
  43.    echo "<script>window.location.href='index.php?page=list-album';</script>";
  44.    exit;
  45. }
  46. <!-- google_ad_section_end -->
  47.  
  48.  
  49.  
  50.  <FORM enctype="multipart/form-data" action="addalbum.php" method="post">
  51.     <P>
  52.     <LABEL for="firstname">al_name: </LABEL>
  53.               <INPUT type="text" name="form" id="albumName"><BR>
  54.     <LABEL for="lastname">al_description: </LABEL>
  55.               <INPUT type="text" name="form" id="albumDesc"><BR>
  56.     <LABEL for="email">al_date: </LABEL>
  57.               <INPUT type="text"  id="NOW"><BR>
  58.                             <input type="file" name="newName" class="input">
  59.                 <p><input type="submit" name="submit" value="Upload" class="submit"></p>
  60.  
  61.     </P>
  62.  </FORM>
  63.  

funtions.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. /*
  4.  
  5. Upload an image and create the thumbnail. The thumbnail is stored
  6.  
  7. under the thumbnail sub-directory of $uploadDir.
  8.  
  9. Return the uploaded image name and the thumbnail also.
  10.  
  11. */
  12.  
  13. function uploadImage($inputName, $uploadDir)
  14.  
  15. {
  16.  
  17.  
  18. $image     = $_FILES[$inputName];
  19.  
  20. $imagePath = '';
  21.  
  22. $thumbnailPath = '';
  23.  
  24. // if a file is given
  25.  
  26. if (trim($image['tmp_name']) != '') {
  27.  
  28. $ext = substr(strrchr($image['name'], "."), 1);
  29.  
  30. // generate a random new file name to avoid name conflict
  31.  
  32. // then save the image under the new file name
  33.  
  34. $imagePath = md5(rand() * time()) . ".$ext";
  35.  
  36. $result    = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
  37.  
  38. if ($result) {
  39.  
  40. // create thumbnail
  41.  
  42. $thumbnailPath =  md5(rand() * time()) . ".$ext";
  43.  
  44. $result = createThumbnail($uploadDir . $imagePath, $uploadDir . 'thumbnail/' . $thumbnailPath, THUMBNAIL_WIDTH);
  45.  
  46. // create thumbnail failed, delete the image
  47.  
  48. if (!$result) {
  49.  
  50. unlink($uploadDir . $imagePath);
  51.  
  52. $imagePath = $thumbnailPath = '';
  53.  
  54. } else {
  55.  
  56. $thumbnailPath = $result;
  57.  
  58. }
  59.  
  60. } else {
  61.  
  62. // the image cannot be uploaded
  63.  
  64. $imagePath = $thumbnailPath = '';
  65.  
  66. }
  67.  
  68. }
  69.  
  70. return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
  71.  
  72. }
  73.  
  74. /*
  75.  
  76. Create a thumbnail of $srcFile and save it to $destFile.
  77.  
  78. The thumbnail will be $width pixels.
  79.  
  80. */
  81.  
  82. function createThumbnail($srcFile, $destFile, $width, $quality = 75)
  83.  
  84. {
  85.  
  86. $thumbnail = '';
  87.  
  88. if (file_exists($srcFile)  && isset($destFile))
  89.  
  90. {
  91.  
  92. $size        = getimagesize($srcFile);
  93.  
  94. $w           = number_format($width, 0, ',', '');
  95.  
  96. $h           = number_format(($size[1] / $size[0]) * $width, 0, ',', '');
  97. $thumbnail =  copyImage($srcFile, $destFile, $w, $h, $quality);
  98.  
  99. }
  100.  
  101. // return the thumbnail file name on sucess or blank on fail
  102.  
  103. return basename($thumbnail);
  104.  
  105. }
  106.  
  107. /*
  108.  
  109. Copy an image to a destination file. The destination
  110.  
  111. image size will be $w X $h pixels
  112.  
  113. */
  114.  
  115. function copyImage($srcFile, $destFile, $w, $h, $quality = 75)
  116.  
  117. {
  118.  
  119.    $tmpSrc     = pathinfo(strtolower($srcFile));
  120.  
  121.    $tmpDest    = pathinfo(strtolower($destFile));
  122.  
  123.    $size       = getimagesize($srcFile);
  124.  
  125.    if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
  126.  
  127.    {
  128.  
  129.       $destFile  = substr_replace($destFile, 'jpg', -3);
  130.  
  131.       $dest      = imagecreatetruecolor($w, $h);
  132.  
  133.       //imageantialias($dest, TRUE);
  134.  
  135.    } elseif ($tmpDest['extension'] == "png") {
  136.  
  137.       $dest = imagecreatetruecolor($w, $h);
  138.  
  139.       //imageantialias($dest, TRUE);
  140.  
  141.    } else {
  142.  
  143.      return false;
  144.  
  145.    }
  146.  
  147.    switch($size[2])
  148.  
  149.    {
  150.  
  151.       case 1:       //GIF
  152.  
  153.           $src = imagecreatefromgif($srcFile);
  154.  
  155.           break;
  156.  
  157.       case 2:       //JPEG
  158.  
  159.           $src = imagecreatefromjpeg($srcFile);
  160.  
  161.           break;
  162.  
  163.       case 3:       //PNG
  164.  
  165.           $src = imagecreatefrompng($srcFile);
  166.  
  167.           break;
  168.  
  169.       default:
  170.  
  171.           return false;
  172.  
  173.           break;
  174.  
  175.    }
  176.  
  177.    imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
  178.  
  179.    switch($size[2])
  180.  
  181.    {
  182.  
  183.       case 1:
  184.  
  185.       case 2:
  186.  
  187.           imagejpeg($dest,$destFile, $quality);
  188.  
  189.           break;
  190.  
  191.       case 3:
  192.  
  193.           imagepng($dest,$destFile);
  194.  
  195.    }
  196.  
  197.    return $destFile;
  198.  
  199. }
  200.  
  201. /*
  202.  
  203. Check if the user is logged in or not
  204.  
  205. */
  206.  
  207. function checkLogin()
  208.  
  209. {
  210.  
  211. if (!isset($_SESSION['isLogin']) || $_SESSION['isLogin'] == false) {
  212.  
  213. header('Location: login.php');
  214.  
  215. exit;
  216.  
  217. }
  218.  
  219. }
  220.  
  221. /*
  222.  
  223. Create the link for moving from one page to another
  224.  
  225. */
  226.  
  227. function getPagingLink($totalResults, $pageNumber, $itemPerPage = 10, $strGet = '')
  228.  
  229. {
  230.  
  231. $pagingLink    = '';
  232.  
  233. $totalPages    = ceil($totalResults / $itemPerPage);
  234.  
  235. // how many link pages to show
  236.  
  237. $numLinks      = 10;
  238.  
  239. // create the paging links only if we have more than one page of results
  240.  
  241. if ($totalPages > 1) {
  242.  
  243. $self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;
  244.  
  245. // print 'previous' link only if we're not
  246.  
  247. // on page one
  248.  
  249. if ($pageNumber > 1) {
  250.  
  251. $page = $pageNumber - 1;
  252.  
  253. if ($page > 1) {
  254.  
  255. $prev = " <a href=\"$self?pageNum=$page&$strGet\">[Prev]</a> ";
  256.  
  257. } else {
  258.  
  259. $prev = " <a href=\"$self?$strGet\">[Prev]</a> ";
  260.  
  261. }
  262.  
  263. $first = " <a href=\"$self?$strGet\">[First]</a> ";
  264.  
  265. } else {
  266.  
  267. $prev  = ''; // we're on page one, don't show 'previous' link
  268.  
  269. $first = ''; // nor 'first page' link
  270.  
  271. }
  272.  
  273. // print 'next' link only if we're not
  274.  
  275. // on the last page
  276.  
  277. if ($pageNumber < $totalPages) {
  278.  
  279. $page = $pageNumber + 1;
  280.  
  281. $next = " <a href=\"$self?pageNum=$page&$strGet\">[Next]</a> ";
  282.  
  283. $last = " <a href=\"$self?pageNum=$totalPages&$strGet\">[Last]</a> ";
  284.  
  285. } else {
  286.  
  287. $next = ''; // we're on the last page, don't show 'next' link
  288.  
  289. $last = ''; // nor 'last page' link
  290.  
  291. }
  292.  
  293. $start = $pageNumber - ($pageNumber % $numLinks) + 1;
  294.  
  295. $end   = $start + $numLinks - 1;
  296.  
  297. $end   = min($totalPages, $end);
  298.  
  299. $pagingLink = array();
  300.  
  301. for($page = $start; $page <= $end; $page++) {
  302.  
  303. if ($page == $pageNumber) {
  304.  
  305. $pagingLink[] = " $page ";   // no need to create a link to current page
  306.  
  307. } else {
  308.  
  309. if ($page == 1) {
  310.  
  311. $pagingLink[] = " <a href=\"$self?$strGet\">$page</a> ";
  312.  
  313. } else {
  314.  
  315. $pagingLink[] = " <a href=\"$self?pageNum=$page&$strGet\">$page</a> ";
  316.  
  317. }
  318.  
  319. }
  320.  
  321. }
  322.  
  323. $pagingLink = implode(' | ', $pagingLink);
  324.  
  325. // return the page navigation link
  326.  
  327. $pagingLink = $first . $prev . $pagingLink . $next . $last;
  328.  
  329. }
  330.  
  331. return $pagingLink;
  332.  
  333. }
  334.  
  335. /*
  336.  
  337. Display the breadcrumb navigation on top of the gallery page
  338.  
  339. */
  340.  
  341. function showBreadcrumb()
  342.  
  343. {
  344.  
  345. if (isset($_GET['album'])) {
  346.  
  347. $album = $_GET['album'];
  348.  
  349. $sql  = "SELECT al_name
  350.  
  351.                 FROM tbl_album
  352.  
  353.         WHERE al_id = $album";
  354.  
  355. $result = mysql_query($sql) or die('Error, get album name failed. ' . mysql_error());
  356.  
  357. $row = mysql_fetch_assoc($result);
  358.  
  359. echo ' > <a href="index.php?page=list-image&album=' . $album . '">' . $row['al_name'] . '</a>';
  360.  
  361. if (isset($_GET['image'])) {
  362.  
  363. $image = $_GET['image'];
  364.  
  365. $sql  = "SELECT im_title
  366.  
  367. FROM tbl_image
  368.  
  369. WHERE im_id = $image";
  370.  
  371. $result = mysql_query($sql) or die('Error, get image name failed. ' . mysql_error());
  372.  
  373. $row = mysql_fetch_assoc($result);
  374.  
  375. echo ' > <a href="index.php?page=image-detail&album=' . $album . '&image=' . $image . '">' . $row['im_title'] . '</a>';
  376.  
  377. }
  378.  
  379. }
  380.  
  381. }
  382.  
  383. ?>
config.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. session_start();
  4.  
  5. // db properties
  6.  
  7.  
  8.  
  9.  
  10. $dbhost = 'localhost';
  11.  
  12. $dbuser = 'root';
  13.  
  14. $dbpass = '';    
  15.  
  16. $dbname = 'rock';
  17.  
  18. // an album can have an image used as thumbnail
  19.  
  20. // we save the album image here
  21.  
  22. define('ALBUM_IMG_DIR', 'C:/webroot/image');
  23.  
  24. // all images inside an album are stored here
  25.  
  26. define('GALLERY_IMG_DIR', 'C:/webroot/gallery/images/gallery/');
  27.  
  28. // When we upload an image the thumbnail is created on the fly
  29.  
  30. // here we set the thumbnail width in pixel. The height will
  31.  
  32. // be adjusted proportionally
  33.  
  34. define('THUMBNAIL_WIDTH', 100);
  35.  
  36. // make a connection to mysql here
  37.  
  38. $conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error());
  39.  
  40. mysql_select_db ($dbname) or die ("I cannot select the database '$dbname' because: " . mysql_error());
  41.  
  42. ?> 
Aug 27 '10 #1
4 1592
TheServant
1,168 Recognized Expert Top Contributor
As much as I love reading through code, could you please define the problem clearer. Any error message/output, and/or what is is doing that it shouldn't, or not doing that it should.
Aug 27 '10 #2
lisa007
29 New Member
Hi the code meant to upload the details to mYSQL and add image to image folder but it just says error uploading file
Aug 27 '10 #3
TheServant
1,168 Recognized Expert Top Contributor
So the code that's failing is:
Expand|Select|Wrap|Line Numbers
  1. // resize all album image
  2. $result = createThumbnail($tmpName, $imgPath, THUMBNAIL_WIDTH); 
  3.    if (!$result) {
  4.       echo "Error uploading file";
  5.       exit;
  6.    }
2 things to check:
Make sure your GD Library is installed/enabled so the function createThumbnail () works.
Also, echo each of your inputs ($tmpName, $imgPath, THUMBNAIL_WIDTH ) so you know that they have all been set. Look for errors like missing "/" in your path.

See how you go with that first.
Aug 27 '10 #4
johny10151981
1,059 Top Contributor
make sure your form is declared properly.
Aug 27 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2404
by: Franco, Gustavo | last post by:
Hi, I have a question, and please I need a answer. How can I finalize a thread running with Application.Run (I need the message loop!!!) without call Thread.Abort?. I want to call Application.ExitThread in the same thread that it is running.
3
1274
by: Kevin | last post by:
hi I want to create an upload image page. how do I do that?
0
926
by: John Dann | last post by:
Simple form with 2 buttons: Click event for btn1 launches OFD with .initialdirectory explicitly set in code. Click event for btn2 also launches the same OFD but with a different ..initialdirectory explicitly set in code. If I run the form then whichever button I click first runs fine and exactly as expected. But then clicking the other button is still
15
5336
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run from the upload function: A generic error occurred in GDI+. Not sure what exactly that means. From what...
0
1081
by: John Dann | last post by:
I have a program that needs to access a prewritten external data file that is supplied with the program. I want to place this data file in Environment.SpecialFolder.LocalApplicationData\MyProgramFolder\ In the program's setup project I've done: Add Special Folder | User's Application Data File, then within this created a folder called MyProgramFolder, then Add File and pointed to a copy of my external data file, which duly appears in the...
2
2568
by: Tarik Monem | last post by:
OK! I've gone through a few tutorials and I cannot understand what I'm doing wrong casting_registration.php <table> <tr> <td> <form enctype="multipart/form-data" action="thankyou.php" method="post" name="registrationform"> Choose a shows:
0
1993
by: aris1234 | last post by:
hello.. How to upload image file in page update ..?? i have logic like this : if user upload new image then old image must delete and update DB used new name if user not upload new image then old image no delete. code form like this (i dontknow this code right or wrong, please correction if wrong) :
7
2358
by: xx75vulcan | last post by:
Hi, I've got a PHP Upload Form that works great, unless that is, the image your uploading has been modified through a photo editing software. Example: if I upload the image straight from a camera or other, it uploads fine. However, If I want to rotate the image, or resize it with photoshop or simmilar, making sure to save it again as a jpg, the PHP upload won't upload the image.
5
1292
by: rahia307 | last post by:
hi i want to upload image with epoch date Concatenate. image is load successfully. but when i does not want to image upload then epoch date is store in database i use this code if (isset($_FILES)) { $mode = '0777'; $photo1 = $epoch.$_FILES; $image1 = $uploadDir.$photo1;
5
1778
by: rahia307 | last post by:
Hi I am using this code for upload image. <table> <form action="process.php method="POST" enctype="multipart/form-data"> <tr> <td align="right">Image1 :</td> <td><input type="file" name="photo1" class="box"> </td> </tr> <tr>
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.