| |
| | |

CodersClub

 Forgot password?
 Register
Search
View: 3369|Reply: 0
Collapse the left

PHP Code

[Copy link]

 India

Post time: 2012-12-27 16:25 From the mobile phone
| Show all posts |Read mode
Edited by Robin at 2012-12-29 09:54

Here Iam Again With A New doubt!
I Have Got This php code from my friend! And he says he got this from php.net
i dont know how to add it, or how this works! Please help me!
Php code for mobile avatar uploading option
  1. <?php
  2. /***
  3.     this is a simple and complete function and
  4.     the easyest way i have found toallow you
  5.     to add an image to a form that the user can
  6.     verify before submiting
  7.     if the user do not want this image and change
  8.     his mind he can reupload a new image and we
  9.     will delete the last
  10.     i have added the debug if!move_uploaded_file
  11.     so you can verify the result withyour
  12.     directory and you can use this function to
  13.     destroy the last upload without uploading
  14.     again if you want too, just add a value...
  15. ***/
  16. function upload_back () { global $globals ;
  17. /***
  18.     1rst set the images dir and declare a files
  19.     array we will have to loop the images
  20.     directory to write a new name for our picture
  21. ***/
  22. $uploaddir = 'images_dir/' ; $dir = opendir ( $uploaddir );
  23. $files = array();
  24. /***
  25.     if we are on a form who allow to reedit the
  26.     posted vars we can save the image previously
  27.     uploaded if the previous uploadwas successfull.
  28.     so declare that value into a global var, we
  29.     will rewrite that value in a hidden input later
  30.     to post it again if we do not need to rewrite
  31.     the image after the new uploadand just... save
  32.     the value...
  33. ***/
  34. if(!empty( $_POST [ 'attachement_loos' ])) { $globals [ 'attachement' ] = $_POST [ 'attachement_loos' ]; }
  35. /***
  36.     now verify if the file exists, just verify
  37.     if the 1rst array is not empty. else you
  38.     can do what you want, that form allows to
  39.     use a multipart form, for exemple for a
  40.     topic on a forum, and then to post an
  41.     attachement with all our other values
  42. ***/
  43. if(isset( $_FILES [ 'attachement' ])&& !empty( $_FILES [ 'attachement' ][ 'name' ])) {
  44. /***
  45.     now verify the mime, i did not find
  46.     something more easy than verify the
  47.     'image/' ty^pe. if wrong tell it!
  48. ***/
  49. if(! eregi ( 'image/' , $_FILES [ 'attachement' ][ 'type' ])) {
  50.       echo 'The uploaded file is not an image please upload a valide file!' ;
  51.     } else {
  52. /***
  53.     else we must loop our upload folder to find
  54.     the last entry the count will tell us and will
  55.     be used to declare the new name of the new
  56.     image. we do not want to rewrite a previously
  57.     uploaded image
  58. ***/
  59. while( $file = readdir ( $dir )) { array_push ( $files , " $file " ); echo $file ; } closedir ( $dir );
  60. /***
  61.     now just rewrite the name of our uploaded file
  62.     with the count and the extension, strrchr will
  63.     return us the needle for the extension
  64. ***/
  65. $_FILES [ 'attachement' ][ 'name' ] = ceil ( count ( $files )  '1' ). '' . strrchr ( $_FILES [ 'attachement' ][ 'name' ], '.' );
  66. $uploadfile = $uploaddir . basename ( $_FILES [ 'attachement' ][ 'name' ]);
  67. /***
  68.     do same for the last uploaded file, just build
  69.     it if we have a previously uploaded file
  70. ***/
  71. $previousToDestroy = empty( $globals [ 'attachement' ]) &&!empty( $_FILES [ 'attachement' ][ 'name' ]) ? '' : $uploaddir . $files [ ceil ( count ( $files )- '1' )];
  72. // now verify if file was successfully uploaded
  73. if(! move_uploaded_file ( $_FILES [ 'attachement' ][ 'tmp_name' ], $uploadfile )) {
  74. echo '<pre>
  75. Your file was not uploaded pleasetry again
  76. here are your debug informations:
  77. ' . print_r ( $_FILES ) . '
  78. </pre>' ;
  79.       } else {
  80.           echo 'image succesfully uploaded!' ;
  81.       }
  82. /***
  83.     and reset the globals vars if we maybe want to
  84.     reedit the form: first the new image, second
  85.     delete the previous....
  86. ***/
  87. $globals [ 'attachement' ] = $_FILES [ 'attachement' ][ 'name' ];
  88.         if(!empty( $previousToDestroy )) { unlink ( $previousToDestroy ); }
  89.     }
  90.   }
  91. }
  92. upload_back ();
  93. /***
  94.     now the form if you need it (with the global...):
  95.     just add the hidden input whenyou write your
  96.     preview script and... in the original form but!
  97.     if you have send a value to advert your script
  98.     than we are remaking the form.for exemple with a
  99.     hidden input with "reedit" as value  or with a
  100.     $_GET method who can verify that condition
  101. ***/
  102. echo '<form action="" method="post" enctype="multipart/form-data">
  103.   <input type="file" name="attachement" name="attachement"></input>
  104.   <input type="hidden" name="attachement_loos" name="attachement_loos" value="' , $globals [ 'attachement' ], '"></input>
  105.   <input type="submit" value="submit"></input>
  106. </form>' ;
  107. ?>
Copy the Code
You have to log in before you can reply Login | Register

Points Rules

Archive|Mobile|Dark room|CodersClub

Top.Mail.Ru
Top.Mail.Ru

2024-09-29 02:19 GMT+3 , Processed in 0.027881 sec., 9 queries .

Powered by Discuz! X3.4 Release 20230520

© 2001-2024 Discuz! Team.

MultiLingual version, Rev. 4301, © codersclub.org

Quick Reply To Top Return to the list