473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

29
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 1579
TheServant
1,168 Expert 1GB
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
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 Expert 1GB
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 1GB
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
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...
3
by: Kevin | last post by:
hi I want to create an upload image page. how do I do that?
0
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...
15
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...
0
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...
2
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"...
0
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...
7
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...
5
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...
5
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"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.