| | |
Problem in uploading images
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2008
Posts: 47
Reputation:
Solved Threads: 0
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
thanks in advance
Punitha pary
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)
<?php $command=$_POST["command"]; If($command == "") { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> <HEAD> <TITLE>File Uploading Interface</TITLE> </HEAD> <BODY> <CENTER> <BR><BR> <FORM ENCTYPE="multipart/form-data" NAME=MyForm ACTION="" METHOD="POST"> <INPUT TYPE="hidden" NAME="command" VALUE="1"> <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="10000000000"> <TABLE> <TR> <TD>Choose File</TD> <TD><INPUT NAME="MyFile" TYPE="File"></TD> </TR> <TR> <TD COLSPAN="2"><INPUT NAME="submit" VALUE="Upload" TYPE="submit"></TD> </TR> </TABLE> </FORM> </CENTER> </BODY> </HTML> <?php } else { $DestinationDir = "F:\wamp\www\surveys-html/"; $DestinationFile = $_FILES['MyFile']['name']; if (move_uploaded_file($_FILES['MyFile']['tmp_name'], $DestinationFile)) { echo "File uploaded successfully."; } else { switch($_FILES['MyFile']['error']) { case 1 : echo "1The uploaded file exceeds the upload_max_filesize directive in php.ini."; break; case 2 : echo "1The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form."; break; case 3 : echo "1The uploaded file was only partially uploaded."; break; case 4 : echo "1No file was uploaded."; break; case 6 : echo "1Missing a temporary folder."; break; case 7 : echo "1Failed to write file to disk"; break; case 8 : echo "File upload stopped by extension"; break; } } } ?>
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.
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.
I hope it would help you.
I couldn't see any wrong in your code.
Stuck in the middle of Java/PHP/Ajax development?
For free help on any technology and free articles visit http://www.techcubetalk.com
For free help on any technology and free articles visit http://www.techcubetalk.com
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.
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)
<?php $upldDir = 'F:\wamp\www\surveys-html/'; $allowed = array( 'jpg','jpeg','gif','png','doc','docx','xls','ppt','pptx','pdf','zip','tiff' ); $maxSize = 10000000; function getExtension( $file ) { $i = strrpos( $file,'.' ); if ( !$i ) { return ""; } $l = strlen( $file ) - $i; $ext = substr( $file,$i + 1,$l ); return $ext; } if ( isset( $_POST['submit'] ) ) { if ( count( $_FILES ) > 0 ) { $fileName = $_FILES['file']['name']; $tempName = $_FILES['file']['tmp_name']; if ( !is_uploaded_file( $tempName ) ) { $result = "File is not a valid uploaded file"; } else { $exten = getExtension( $fileName ); if ( !in_array( $exten,$allowed ) ) { $result = "File has an unknown extension"; } else { $fileSize = filesize( $tempName ); if ( $fileSize > $maxSize ) { $result = "File exceeds the max file size limit"; } else { $upldDir = $upldDir . basename( $fileName ); if ( !copy( $tempName,$upldDir ) ) { $result = "Unable to upload file"; } else { $result = "File uploaded successfully!"; } } } } } } $thisPage = $_SERVER['PHP_SELF']; $html =<<<HTML <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> <HEAD> <TITLE>File Uploading Interface</TITLE> </HEAD> <BODY> <CENTER> <BR><BR> <FORM ENCTYPE="multipart/form-data" NAME="MyForm" ACTION="{$thisPage}" METHOD="POST"> <TABLE> <TR> <TD>Choose File</TD> <TD><INPUT NAME="file" TYPE="File"></TD> </TR> <TR> <TD COLSPAN="2"><INPUT NAME="submit" VALUE="Upload" TYPE="submit"></TD> </TR> <TR> <TD COLSPAN="2">{$result}</TD> </TR> </TABLE> </FORM> </CENTER> </BODY> </HTML> HTML; echo $html; ?>
Last edited by kkeith29; Nov 19th, 2008 at 12:08 am.
•
•
Join Date: Nov 2008
Posts: 11
Reputation:
Solved Threads: 1
hi
let me try this simple one
let me try this simple one
php Syntax (Toggle Plain Text)
<?php mysql_connect("localhost","root","rootwdp"); mysql_select_db("associates") or die("unable to select"); if(isset($_POST["update"])) { $empid=$_POST['empid']; $username=$_POST['uname']; $dofjoin=$_POST['date']; $design=$_POST['design']; $exp=$_POST['exp']; $contact=$_POST['contact']; $pname=$_POST['pname']; $photo=$_POST['userfile']; if($_FILES['userfile']['size'] > 0) { $file_dir = "photos"; foreach($_FILES as $file_name => $file_array) $fileName = $_FILES['userfile']['name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $tmpName = $_FILES['userfile']['tmp_name']; $file_array; $fileName = $empid.".jpg"; if (is_uploaded_file($file_array['tmp_name'])) { $test=move_uploaded_file($file_array['tmp_name'],"$file_dir/$fileName") or die ("Couldn't copy"); } } if(@$test) { $photo=$file_dir."/".$fileName; } //echo "<img src='".$_SESSION['photopath']."'/>"; //echo "<a href='#' onclick='refreshParent()'>close</a>"; $remarks=$_POST['remarks']; $query = "UPDATE `jos_employee` SET name='$username', designation='$design', date_of_joining='$dofjoin', experience='$exp', contact='$contact', remarks='$remarks' "; if($test) { $query.= "photo_url ='$photo'"; } else { } $query.=" WHERE emp_id=$empid"; $run=mysql_query($query); //header("Location:index.php?option=com_testingofupdatedelete&page=$currpage"); header("Location:index.php?option=com_completedetailsofassociate"); }?> <?php mysql_connect("localhost","root","rootwdp"); mysql_select_db("associates") or die("unable to select"); $id=$_GET['id']; $query = "SELECT * FROM `jos_employee` WHERE emp_id='$id'"; $dd=mysql_query($query); ?> <form action="" method="post" name="myform" enctype="multipart/form-data"> <table bgcolor="#CDE3EE" align="center" class="tablecls" > <?php while( $row=mysql_fetch_array($dd)) { ?> <tr> <td>Username : </td> <td><input type="text" value="<?php echo $row[0];?>" name="uname" id="uname" /></td> </tr> <tr> <td>Emp Id : </td> <td><input type="text" value="<?php echo $row[1];?>" name="empid" id="empid" /></td> </tr> <tr> <td>Designation : </td> <td> <input type="text" value="<?php echo $row[2];?>" name="design" id="design" /></td> </tr> <tr> <td>Date Of Join : </td> <td><input type="text" value="<?php echo $row[3];?>" name="date" id="date" /></td> </tr> <tr> <td>Experience : </td> <td> <input type="text" value="<?php echo $row[4];?>" name="exp" id="exp" /></td> </tr> <tr> <td>Contact Number : </td> <td><input type="text" value="<?php echo $row[5];?>" name="contact" id="contact" /></td> </tr> <tr><td>Photo</td> <td colspan="2"> <img src="http://localhost/associates/<?php echo $row[6];?>" /> </td><td><input type="file" id="userfile" name="userfile" /></td></tr> <tr> <td>Remarks : </td> <td> <input type="text" value="<?php echo $row[7];?>" name="remarks" id="remarks" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="update" id="update" value="Update"/> <input type="reset" id="reset" name="reset"></td> </tr> <?php } ?> </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.
![]() |
Similar Threads
- image upload problem (PHP)
- Problem uploading images? (DaniWeb Community Feedback)
- uploading Images into Database (ASP.NET)
- File upload problem (PHP)
- Uploading images to the Web Server using ASP (ASP)
- problem uploading org chart with live link to intranet (Software Development Job Offers)
- Internet graphics display problem (OS X)
Other Threads in the PHP Forum
- Previous Thread: Array Values help
- Next Thread: displaying random images
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date development directory display download dynamic echo email error file files filter folder form forms function functions gc_maxlifetime google host href htaccess html image include insert integration ip java javascript joomla limit link login loop mail memmory memory menu mlm mod_rewrite multiple mysql navigation oop parse parsing paypal pdf php problem query radio random recursion regex remote script search server sessions sms snippet soap source space sql structure syntax system table thesishelp tutorial update upload url validation validator variable video web xml youtube






