943,866 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 959
  • PHP RSS
Nov 18th, 2008
0

Problem in uploading images

Expand Post »
Hi,

I trying to creat code to upload images .in my code i can upload files but i cant upload images can any one tell me what is the problem in my code


this is my code

php Syntax (Toggle Plain Text)
  1. <?php
  2. $command=$_POST["command"];
  3. If($command == "") {
  4. ?>
  5. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  6. <HTML>
  7. <HEAD>
  8. <TITLE>File Uploading Interface</TITLE>
  9. </HEAD>
  10. <BODY>
  11. <CENTER>
  12. <BR><BR>
  13. <FORM ENCTYPE="multipart/form-data" NAME=MyForm ACTION="" METHOD="POST">
  14. <INPUT TYPE="hidden" NAME="command" VALUE="1">
  15. <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="10000000000">
  16. <TABLE>
  17. <TR>
  18. <TD>Choose File</TD>
  19. <TD><INPUT NAME="MyFile" TYPE="File"></TD>
  20. </TR>
  21. <TR>
  22. <TD COLSPAN="2"><INPUT NAME="submit" VALUE="Upload" TYPE="submit"></TD>
  23. </TR>
  24. </TABLE>
  25. </FORM>
  26. </CENTER>
  27. </BODY>
  28. </HTML>
  29. <?php
  30. } else {
  31. $DestinationDir = "F:\wamp\www\surveys-html/";
  32. $DestinationFile = $_FILES['MyFile']['name'];
  33. if (move_uploaded_file($_FILES['MyFile']['tmp_name'], $DestinationFile)) {
  34. echo "File uploaded successfully.";
  35. } else {
  36. switch($_FILES['MyFile']['error']) {
  37. case 1 : echo "1The uploaded file exceeds the upload_max_filesize directive in php.ini.";
  38. break;
  39. case 2 : echo "1The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";
  40. break;
  41. case 3 : echo "1The uploaded file was only partially uploaded.";
  42. break;
  43. case 4 : echo "1No file was uploaded.";
  44. break;
  45. case 6 : echo "1Missing a temporary folder.";
  46. break;
  47. case 7 : echo "1Failed to write file to disk";
  48. break;
  49. case 8 : echo "File upload stopped by extension";
  50. break;
  51. }
  52. }
  53. }
  54. ?>

thanks in advance
Punitha pary
Last edited by peter_budo; Nov 20th, 2008 at 12:44 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
punithapary is offline Offline
48 posts
since Jun 2008
Nov 18th, 2008
0

Re: Problem in uploading images

I wrote a similar code about image upload and gave a detailed explanation about what the code does at any given section. You can find it here at Daniweb: http://www.daniweb.com/forums/thread148350.html

I hope it would help you.
Reputation Points: 16
Solved Threads: 9
Junior Poster
mexabet is offline Offline
148 posts
since Mar 2008
Nov 18th, 2008
0

Re: Problem in uploading images

I couldn't see any wrong in your code.
Reputation Points: 13
Solved Threads: 1
Newbie Poster
jack239 is offline Offline
14 posts
since Aug 2008
Nov 19th, 2008
0

Re: Problem in uploading images

in the move_uploaded_file() function you are moving the file to the file name itself without the upload directory prepended to it. you need to specify the directory the file needs to go to.

i rewrote your code. i made i a lot more secure. its untested, please let me know of any errors.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $upldDir = 'F:\wamp\www\surveys-html/';
  4. $allowed = array( 'jpg','jpeg','gif','png','doc','docx','xls','ppt','pptx','pdf','zip','tiff' );
  5. $maxSize = 10000000;
  6.  
  7. function getExtension( $file ) {
  8. $i = strrpos( $file,'.' );
  9. if ( !$i ) {
  10. return "";
  11. }
  12. $l = strlen( $file ) - $i;
  13. $ext = substr( $file,$i + 1,$l );
  14. return $ext;
  15. }
  16.  
  17. if ( isset( $_POST['submit'] ) ) {
  18. if ( count( $_FILES ) > 0 ) {
  19. $fileName = $_FILES['file']['name'];
  20. $tempName = $_FILES['file']['tmp_name'];
  21. if ( !is_uploaded_file( $tempName ) ) {
  22. $result = "File is not a valid uploaded file";
  23. }
  24. else {
  25. $exten = getExtension( $fileName );
  26. if ( !in_array( $exten,$allowed ) ) {
  27. $result = "File has an unknown extension";
  28. }
  29. else {
  30. $fileSize = filesize( $tempName );
  31. if ( $fileSize > $maxSize ) {
  32. $result = "File exceeds the max file size limit";
  33. }
  34. else {
  35. $upldDir = $upldDir . basename( $fileName );
  36. if ( !copy( $tempName,$upldDir ) ) {
  37. $result = "Unable to upload file";
  38. }
  39. else {
  40. $result = "File uploaded successfully!";
  41. }
  42. }
  43. }
  44. }
  45. }
  46. }
  47.  
  48. $thisPage = $_SERVER['PHP_SELF'];
  49.  
  50. $html =<<<HTML
  51. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  52. <HTML>
  53. <HEAD>
  54. <TITLE>File Uploading Interface</TITLE>
  55. </HEAD>
  56. <BODY>
  57. <CENTER>
  58. <BR><BR>
  59. <FORM ENCTYPE="multipart/form-data" NAME="MyForm" ACTION="{$thisPage}" METHOD="POST">
  60. <TABLE>
  61. <TR>
  62. <TD>Choose File</TD>
  63. <TD><INPUT NAME="file" TYPE="File"></TD>
  64. </TR>
  65. <TR>
  66. <TD COLSPAN="2"><INPUT NAME="submit" VALUE="Upload" TYPE="submit"></TD>
  67. </TR>
  68. <TR>
  69. <TD COLSPAN="2">{$result}</TD>
  70. </TR>
  71. </TABLE>
  72. </FORM>
  73. </CENTER>
  74. </BODY>
  75. </HTML>
  76. HTML;
  77.  
  78. echo $html;
  79.  
  80. ?>
Last edited by kkeith29; Nov 19th, 2008 at 12:08 am.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Nov 21st, 2008
0

Re: Problem in uploading images

hi

let me try this simple one

php Syntax (Toggle Plain Text)
  1. <?php
  2. mysql_connect("localhost","root","rootwdp");
  3. mysql_select_db("associates") or die("unable to select");
  4. if(isset($_POST["update"]))
  5. {
  6. $empid=$_POST['empid'];
  7. $username=$_POST['uname'];
  8. $dofjoin=$_POST['date'];
  9. $design=$_POST['design'];
  10. $exp=$_POST['exp'];
  11. $contact=$_POST['contact'];
  12. $pname=$_POST['pname'];
  13. $photo=$_POST['userfile'];
  14. if($_FILES['userfile']['size'] > 0)
  15. {
  16.  
  17. $file_dir = "photos";
  18. foreach($_FILES as $file_name => $file_array)
  19. $fileName = $_FILES['userfile']['name'];
  20. $fileSize = $_FILES['userfile']['size'];
  21. $fileType = $_FILES['userfile']['type'];
  22. $tmpName = $_FILES['userfile']['tmp_name'];
  23. $file_array;
  24. $fileName = $empid.".jpg";
  25. if (is_uploaded_file($file_array['tmp_name']))
  26. {
  27. $test=move_uploaded_file($file_array['tmp_name'],"$file_dir/$fileName") or die ("Couldn't copy");
  28. }
  29. }
  30. if(@$test)
  31. {
  32.  
  33. $photo=$file_dir."/".$fileName;
  34. }
  35. //echo "<img src='".$_SESSION['photopath']."'/>";
  36. //echo "<a href='#' onclick='refreshParent()'>close</a>";
  37.  
  38. $remarks=$_POST['remarks'];
  39. $query = "UPDATE `jos_employee` SET name='$username', designation='$design', date_of_joining='$dofjoin', experience='$exp', contact='$contact', remarks='$remarks' ";
  40. if($test)
  41. {
  42. $query.= "photo_url ='$photo'";
  43. }
  44. else
  45. {
  46.  
  47. }
  48.  
  49. $query.=" WHERE emp_id=$empid";
  50. $run=mysql_query($query);
  51. //header("Location:index.php?option=com_testingofupdatedelete&page=$currpage");
  52. header("Location:index.php?option=com_completedetailsofassociate");
  53.  
  54. }?>
  55.  
  56. <?php
  57. mysql_connect("localhost","root","rootwdp");
  58. mysql_select_db("associates") or die("unable to select");
  59. $id=$_GET['id'];
  60. $query = "SELECT * FROM `jos_employee` WHERE emp_id='$id'";
  61. $dd=mysql_query($query);
  62.  
  63. ?>
  64. <form action="" method="post" name="myform" enctype="multipart/form-data">
  65. <table bgcolor="#CDE3EE" align="center" class="tablecls" >
  66. <?php
  67. while( $row=mysql_fetch_array($dd))
  68. {
  69. ?>
  70. <tr>
  71. <td>Username :&nbsp;&nbsp; </td>
  72. <td><input type="text" value="<?php echo $row[0];?>" name="uname" id="uname" /></td>
  73. </tr>
  74.  
  75. <tr> <td>Emp Id :&nbsp;&nbsp; </td>
  76. <td><input type="text" value="<?php echo $row[1];?>" name="empid" id="empid" /></td>
  77. </tr>
  78. <tr>
  79. <td>Designation :&nbsp;&nbsp; </td>
  80. <td> <input type="text" value="<?php echo $row[2];?>" name="design" id="design" /></td>
  81. </tr>
  82. <tr>
  83. <td>Date Of Join :&nbsp;&nbsp; </td>
  84. <td><input type="text" value="<?php echo $row[3];?>" name="date" id="date" /></td>
  85.  
  86. </tr>
  87.  
  88. <tr>
  89. <td>Experience :&nbsp;&nbsp; </td>
  90. <td> <input type="text" value="<?php echo $row[4];?>" name="exp" id="exp" /></td>
  91. </tr>
  92. <tr>
  93. <td>Contact Number :&nbsp;&nbsp; </td>
  94. <td><input type="text" value="<?php echo $row[5];?>" name="contact" id="contact" /></td>
  95. </tr>
  96. <tr><td>Photo</td>
  97. <td colspan="2">
  98. <img src="http://localhost/associates/<?php echo $row[6];?>" />
  99. </td><td><input type="file" id="userfile" name="userfile" /></td></tr>
  100. <tr>
  101. <td>Remarks :&nbsp;&nbsp; </td>
  102. <td> <input type="text" value="<?php echo $row[7];?>" name="remarks" id="remarks" /></td>
  103. </tr>
  104.  
  105. <tr>
  106. <td colspan="2" align="center"><input type="submit" name="update" id="update" value="Update"/>&nbsp;&nbsp;&nbsp;<input type="reset" id="reset" name="reset"></td>
  107. </tr>
  108. <?php } ?>
  109. </table>
Last edited by peter_budo; Mar 8th, 2009 at 6:40 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
punjabivirsa3 is offline Offline
11 posts
since Nov 2008
Mar 2nd, 2009
0

Re: Problem in uploading images

Hello all , I get "1 Missing a temporary folder." where can I add my temp folder ?

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xantini is offline Offline
1 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Array Values help
Next Thread in PHP Forum Timeline: displaying random images





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC