Solved

How to recreate the following POST request (uploading an image) in PHP using file_get_contents

Posted on 2014-07-26
11
583 Views
Last Modified: 2014-07-26
I want to know how I can upload an image to this server programatically. Here is an example of the image being uploaded:

---------------------------------------------------------------------------------

Host: secure.z57.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://secure.z57.com/mx/mcc/index.cfm?event=listings.updateListing
Cookie: NOTSHOWN=NOTSHOWN
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------319952555227349
Content-Length: 5570


-----------------------------319952555227349\r\n
Content-Disposition: form-data; name="jsCallback"\r\n
\r\n
T\r\n
-----------------------------319952555227349\r\n
Content-Disposition: form-data; name="listItem"\r\n
\r\n
\r\n
-----------------------------319952555227349\r\n
Content-Disposition: form-data; name="IMAGE"\r\n
\r\n
\r\n
-----------------------------319952555227349\r\n
Content-Disposition: form-data; name="NEWFILE"; filename="logo.png"\r\n
Content-Type: image/png\r\n
\r\n
\x89PNG\r\n
\x1a\n
\0\0\0\rIHDR\0\0\0A\0\0\0#\b\x02\0\0\0D©ðC\0\0\0\x01sRGB\0®Î\x1cé\0\0\0\x04gAMA\0\0±\x8f\vüa\x05\0\0\0\tpHYs\0\0\x0eÃ\0\0\x0eÃ\x01Ço¨d\0\0\x13\vIDATXGíWyTTg\x96÷ÿ9Óg2\n
µ¼WË[«\x8aÍ`\x14£Ñ$\x04QTv\x94¥\x80Z¨¢ª XE\\pITÚ=vDq\x89[pCd3¨\x88¢Q#"\x04D\x8d\x89â\x92î\x98¸\0²\x15µïs_aÒ3éé\x9cîÿfæÌwn½zï~۽߽÷wï7Áû¿¿ý¿\x0eÿ3Úÿu\x1dÜ\x1e\x87Çëvz=@.¯Ç\r\f\x8fÃív0}\x9e¿!¯Çã±y½6ß\x18·Ëí1Û\x1c]=\x8f\x9a\x9a¯¼\x1a\x18tºaº×åqÁ\x82\x1e\x8f\x13È·ªÇG¿¬àf:\x19Î?Ù~G\a\x8fÛe\x7fù²ÿÐÑ\x86=\x87Ïî;röÀÑÆGO\x9e1{2[ÂÓý\x86¼@Àry½&§Ëb0\x9a{î>;|ôB\x86²\x1cÅc&¢\x11AÓ\x92\n
\x97U\x1c9yñûG/-6P\0\x06Û}\x9a\x8cK?¾\x8eO\x01F\x877Ûÿãíw}Éåí¹ý\x98+\x88ä\x8b2\x89@5_\x94ØÔ|Ç·/\x88\vGnñxLpÜ.\x97ÕfsØmÞ\x17¯L\a\x0e7/NY\x13\x18¢À(¹\x80Ê\x16\x88sPJçõ¸DÏ'\x95ÁS\x94*íÆÃGÏ\f\x1bL\x0e\x97ËâpÃB.\x8fÍé6ÂjÌ\x9a\x1e\acò\x7f²ý\x8e\x0eà;\x8e\x9e\x9e^6o!Bçð\x83\x8a\x10Qzã\x85»pæN§\vlä\x81\x7f'\x1c§Ç0b¯:Ô¬Vlzg\x86\x1eÅÓY\x88THæ \x98*hê\x92\x8f·´Ô6?øxkË\x94YÅ\x1c¡TH«È\0\x95\x80N\r\x9d¡Ôäo¯®»:4æ\x84\x9d\x1cn»Çk÷\x82+2\x9e\v;üs\x8dÑÁç\n
\x8c\tÿjFæ\rئÛw\x1e²\x84Q\\\x91\x06\rÈåÐ\x89\x8d-ívÐÌé°YÝ\x06\x83£ýæ\x93\x82\x82?\x89Å\x8b1<\x95\x14©øt6\x8fÔ\b%\x05\x81SK§\x7f°j^üº\x19\x91úiá\x8a\x0f\x17æ.\\¼2Nºizør,Pç/P"¤\x96Kª\x11"M\x12\x9a\x91Wüé­®ï­\x16û\x9b}ÝàWp@pLð2Îb¸àg¾Ïß4Fö\tÀwyÁ¦0è\x97ö\x8b¯Ãïν'\\a4J1[\n
Ä©Õ\x8d·\x8c\x0e×\x83Þ\x1f\x97-ß\x1e:%\x95\x12I1\x91\x8aK(øAÙÂ\x10å\a\x1f\x15\x16»x;B*\x11ZæÔ,*\x8b\x85gñ0½\x80Îa\x93é\x1cQòÁÚ\x9e]\x87Û\x03§¨É\x90l4@\x83PY¸8\x8b\x96dL\x99\x92±mëÉW}#6«Ýé°Ú­&\x97\x13üj<ö|"\x8dkðæsü\v~`F÷\x1b;øL\x01\x8e\b,ß\x13@\x83y·÷ÜíåðçqÉl\x0eUÈ!µ\vS¶\x84Ï/¡\x83Õ(\x9eÆ'¥\x02JJHÒ>ø(wíú\x13\x1d]Oûú¬%Kw¢d,\x97Ê`Q*\x7fR7\x89Èf\x13*.)cãr\x0e­æ\x88\x15,2±þ\\×\x88ÉÕÙýdë§ÕQó\x8biQ\x16&Ô\t\x849\x02\\%\tÊ\x9e\x1f½´¢²îîw/FÍ\x80\x8aoÎ\x93\x91\x85Q\x83\x91ø\x8dbL\x83?èûk<\x8c\x8fpx<à\x9a\x80\x9e\x80\x80`\x1d÷7\x9d\x0fQ,\x86Ch¸ôR6½Ä\x0fW¡\x94\x8aGC°jBg-ÉÊÛÓtéÛ¾\x0186\x87ÛéþêB\x0fMDñ\x88ElRæG+X´4ðݬ\x80°L¾(\x83C¨ØT\x11\x87,â`òØE%¯\x87Ç\\N\x1b\x1c×è\x88ùZÛÝ\x95ëöÍ\x9a\x93+\n
Ñ¢\x98\x86\x12\x17£\x10KS´Rõ¦ê/¯?þé\x95É\x02\x06ñº\\N\x10\fÐ\fpûÍ\x99ÿ¢\x8aO\aæ}\\Y@\v\x87Ýî\x013ö÷ÛN\x9e¸\x91\x94¸\x9a\x8bI9T.[T\x80Hr8T\x86P\x9c¢ÖmÙ±»%.å\x8fÉÊíríÖ%+6\x19M£0wóúã\x02~<J*X\x94\x96%R,L]÷à\x87\x91Þ§\x83Ë˪ù\x84\x82K\x16N\x12\x16"¢ì\x90©©½\x8f\x9e\x830ý¯\x06kêêO\x9ei<X]{¸¶ùJÛãµ\x1f\x9f\f\x9e¬áã:\x84Ôð\x025ØdU@\x98<UQvº¾udØêr01ïrº].\x97\x87\x81¯ÿ¬\x03\x130Àg¢Êh´Ô\x9enÕçl\x0e\x9d,%q%O¨âQyl:\x97#V³è¤õ;\x1azÿ2\f.Ûqë±H\x92\x8aàj..\x9f='\x7f`Ä\x0ea¸re\x05G0\x9fEÈÙ¢<\x7fB\x1d63\x7foeÃw=Ïþüxè\x9dé\n
<X\x15ð^ÉÔÈÒy\x89\x85\x1d]\x0fà4»»\x1eð\x84\x91BZÎÇ\x153ßÏ{ñÜl\x1e\x1b\x1dx=\x98(]Æ"\x12Y"=\x1a´\x9a-*\x15Hô\x98X6m\x9atiÑæ/\x1b¾2\x9b\x189]\x90GÝà,\x8c\x1a\x13ÜN\x10ÞÖ÷|àö·Ïô+÷ý\v\x1aåGeðÅ:¡(W\x80åÑ!9¼Àtn \x92Må!¸æTÝw\x8c\x03zìßtöRÁ\x99\x1cq>G¢\x9f=·Øh\x06\x1fõìÜu\fÁ£X\x98Ü\x8fÌcÑz.¥æ i\x7fx+\x06¡¤«6^îønðµ\x8dA\x0e·×â¶ÛÝvwOw/\x0f\x8bãâz?<wú\x87K~~nu\x18GlFC\x8alµ\x1f¦bÑ«&b\x9a°\x0fõ\x81¡\0Ö\x99\\\x91\x1a\rÌ\x9a\x88.\x12IÒË?©ùîî³á!\x93Ãã\x024\x9aàq{~üa j¡\x0e\x17/Â\x83´h@\x0e\x87ÎÆ$êYá%ËË\x0eÝ{د.ÜîO$òDZ\x1e!«ièd0ÎëèìzLNÎ`\x894\x1c\x89fö¼¢1£Çíô|wçypP:\x0fW²\t\x1d"Ñ"b9Be D&\x97T@¦ã\x90ò)ïe\x9d¨¿4b\x1c3\x9b\x8d\0\x9eot 5~¤fzDñ³\x97V\x8baÔj4f*ËXx\x06[¤ç\aJ«k[^\x8f\x18¿8~9Iº\x9e\b\x92á\x81*"(W(ÎÂ%IjÍ:\x8b\x15àÇ;\x01<ëûǯ¦ÏÎB©T¶P\x89\x10ªÀiªãµm¯\x06!w:]nWþÒm<ZÊ#õ<\\VU}sÌb3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f^}úY\x03\x1d\x92È\x15§°E\x90¹u\x1cRº`Qñ°ÉÖÝó\x90\x8f/D\bµ\x1f\x96\x038æ\x87ɸ¸Z@«\x01\x9d\x01*üÅ:®8õXíU\x80|§\a\x1c\x1f"\x0e\x80Ã\x97ó þ\x87swB&\08üû5\x1fôÙA\x05\x97·¨p7\x0f\x93±É\x02\x0eUðîì\x92¨H]Ä\\mØ\ay(Ô¤\x01%¨X÷~D¾Áà|ö\x97¾°©Y\x18)ç\x90\x1a\x8eX\x8fJ2+ö\x9e7\x8e\x9al¦\x11»Á¦ÓnbñS'â¹\x93H\x88õ\x94ØÔâa£íî½\x87B<\x16Á \fÉC\x83²ÄÓå¡3³ß\x9e®¦\x02å(\x91\x8b\a,åÒ\x8b\x0fÔ´@\x85üß\x96J¿¶ßÓÁ§··¤¸R@fò$yÿÆÏ8Qwßnu[Í®k×\x9f\x90Aj®(\x87KeÍ\x9c\x937dp\x9aÌ\x8eè¸R\x94HFÄZ\x7fZïOe\a\x87i¾8~ýÁ\x83ç\x17ÎÞ\t{7\x97\x83©ýñ\x02\x0e\x9dË%\x93KWï\x1d³znß},À\x17¡B=[\x90\x15ö~î\x9f\x7f\x1e\x1e\x1e²\x0e\x8f\x9a\x17gü\x11 \x85'ÊG\x03¥\x87ë.3.\x0fÒü}-~W\aßKiÉ\x1e\x01\x95É¡\x01\x01S\x8f×ßbºÜP\x83<\x11\x87ÈPZǦÕa\x1få\r\x18\x9cà}\x9fí®å`Qþ\x98\x14¼Ù\x8fÊ÷'³ù\x12Y@h\x16!ÊD0%\a×ûãz\x0e©$\x82S®Þzb\x81\x1cw÷©\0K\x16bù|\\=;<{hÄì\x01\x17wº3\x15\x1b¸D&\x9bÖ±\x02¤\x9f×^\x81Ê\x87)ßþq\x1d\x98ðð50\x1fäuà\x94BL\x83\x87й~dú±úvf\x8cÛÕyë\x9188S ÖC!4#"\x7fÈà\x80R¦\x7fÐ2?&\x97G%\x82 \x90ãÀõYt\x16\x8b\x92sÉ,6©a\x11à3j\x7faBáÊ]?¿6\x98]\x8en°\x03¶H\x80å£xö\x8cpõà¨Ù\r÷!§[¡*GI\x19äiVPæçu_1v\0xõ\x85è¯í\x8d\x94ãyz\x9cõk{Ó\x03:0\x11Í\\\x9dKKö\vH%ÄÃ$B~´®k|Э\x8e§¤D\x8e\x10y(®\x9bõaÞ\x88\x81¹\x7fÙlÎGO\x86#ç\x17ú£\t\\ZÅ\x15«'1¢(üh\x8d?\x98\x11Wþ+7ví\x96Ó/Gí\0cv\x8féöÝ\x87\x02<\x96\x8fçq\tíô\x8frú\x8cfØ\x0f.X2°\x03.\x9bDëÿ]\x92¾÷ôe\x88\a_\x10ÿU\t&®\x99«\x01SmÀ'Ô\x1aîû=ß_<\x7fãbswË\x85î³ç¾9{¾íLSGóù®ææösç»\x92S·\v)\x1dJäp\tÙ²µõõçn7µtm­hÂ'gpèL\0ò\x80©úêÆ\x8e//tÔ7ß:uöZÃ\x85.M^\x95(D\x83\x88\x16q)\0\x03ýDBõ\x16-\r\tÏß²»õì¹»gÎv\x9d:wãdó\x8d\x9d\x9f\x9f\x85<ÍÇ\x14\x93ørêÝüý57ê\x9a¾>×Ü>7f\x05\x82É\x85t¾@¢-^}ìô¹\x9b5ç®7\\¸ÙØÜÙØÔÖp¶­®¹íTó×ÕgZ{\x1fþ\b÷¥\t ÊÚu\x87\x11a"à4[(GètD\x9cÈ\x15Ås\b¨dbQ"ÕO \x9f\x88©\x01\x169¢,\x7fL>\x11Í\x84*\x88ÍOà\x8bSxLQ\x9d\x89\x89´\x1cþb\x1e\x0e.\x14ïO\x84ûã1þ\x18sõ\x11\x86ÈY\x10H\x84\x8eCÈÐ ð®T\x0e¶\x98\x87$\v\x05À\x94³ÀD¼T\x8aVã¤\x86Oå³é|\x16®DxIln\x02\x8a«\x10¡Z\x80kqZ\x8bà\x19|"\x9d+LCp\x05O\x90M`Z\x8a(@\x88\x1c?\x02RMlåÞ&°È\x04°Æúòj\x8cVñE\x85\\º@ð¶6E·£²êò¾ª¯*ö\x9c\x0f\x9b\x91\x1b\x12¦ÛVyiÏñ+»\x8e´nÚq!4t¹\0Ó,HX¹­¢qÏ\x17_\x95®ý\x9c\fÈÄiõ\x86\x8d\x8d\a\x0f¶\x1e=qqÝÆ**@õáÜ\x15\x95G.\x1e<vùØÉ\x8e\x8f×\x9dAé$Yî\x9f\x0eV]«®nÛ½ÿJlÆö°y+\x0e\x9c¼~¨ªùPUÓ\x175WÃã\x96\x8a§eì=|I§ßÏ\x11¤\x17-¯:|âFu]gõé®È\x05\x1bEÁú%k\x8e\x1d8qegesTÌZ\x0eÜÉ\x02´\x93¨\x05\x95Gª¡Vbrܦ\x8d\x87I"QHe¡t\x16?(esEõÀ°qOå\x9eþ>Ã\x9e]\x8dÓÞIùáÙȽÞ\x9e\v\x97Ï\x80ó©¥Û§Ï,¾ÿ\x97\x81Æ\x96\x9bkÊ÷\x0e\x98\x8ck7T\x91\x81²¯o=x|ÿÑ¡ý{,&Ïâ¤U)Òe&«åþý»Öm8\x8d\x90\v7ïª\x190\x18kê\x8eüÔÿcK[OÐl\x85B¿£½ûéÏ/\x06\v\x96l\v\x9d©\x9b\x16\x9e×7ìÚ} \x95K/¨=wyÀ8ôu{ûõ¶\a³?Ê\x9b\x9f´æg\x83«µý«Kׯ\x16­ø\x13\x1bÏd\x8b\x8bý\x88ô]\a¿|£ÃÆ\x8d\x87\x04x"\\\x9aýI-/XYþY\r ÆÐë×&\x8bS\x91µnÚôìo{\x87\aÆ\x86\fÖ¾\x13Gk\x83èøÄÔòQ»]\x97·\x9eÇ\x9fýèÙÓ\x86³w0:åjÇ\x9dÁþ\x91×ÃCO\x7f2¼3+71sýË\x11kSóù={«S3ÊQQâºíÕ\x06\x8býÅó\aC\x83}\v¢u<Èý\x82¤s\x17î?þ¾/(P\x0e~2mVéOýöÝ\x87.¢Tü©¦+/û\a*+\x8fïü¬%ô\x1d\r\x11¼X\x9eS~êtË@ÿà\x99¦\x9b\b.ã\x90Ëý\x04êÝ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²¡Ñåq\x81\x1dÜVË\x88ÑøzllxhÌ`0\x9b\r&3\\´Lp×2\x9b\x8cæ±1³Á`\x1a\x1d3\x19Æ̦ñ®1³\x85y\x9a\x8c\x06Ó\x98Ñ\x02Oæb\x06L\x83\tÈl0ÃÈa£yÔh\x82\x11\x16\x18\x01\x13\x7fK&x\x1aÇ\x98Õ\x98Á¾wß0ØÈ<Âì\x05sMV£\x99¹o\x19\x99®1`\x1a\x18þè¨Éd0[ÇÌÖQ\x93Ñâ\x18\x83\x8aÛ\x97§\x99bû\r19\x90©\x13}e\x16|þ\x96|}\fý\x86\x0fä\x9bÂÌr{á\x96Ë\x90\x87)¹ ^ùíÈÿJ0\f\x9e0ù×O\x86|\x13Ç\t¶cÆ\x80|PH3c\x98\x94\rÛ\x8d÷z¼ÿ\x01\x99\x91£×©¾\bë\0\0\0\0IEND®B`\x82\r\n
-----------------------------319952555227349\r\n
Content-Disposition: form-data; name="imgTitle"\r\n
\r\n
\r\n
-----------------------------319952555227349--\r\n

Open in new window

0
Comment
Question by:Mark
  • 4
  • 4
  • 3
11 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40221529
Is your question how to do a file upload?  PHP file_get_contents() is nowhere in that design.  This teaching example shows the essential moving parts.

<?php // demo/upload_single_example.php
error_reporting(E_ALL);


// MANUAL REFERENCE PAGES YOU MUST UNDERSTAND TO UPLOAD FILES
// http://php.net/manual/en/reserved.variables.files.php
// http://php.net/manual/en/features.file-upload.php
// http://php.net/manual/en/features.file-upload.common-pitfalls.php
// http://php.net/manual/en/function.move-uploaded-file.php

// MANUAL PAGES THAT ARE IMPORTANT IF YOU ARE DEALING WITH LARGE FILES
// http://php.net/manual/en/ini.core.php#ini.upload-max-filesize
// http://php.net/manual/en/ini.core.php#ini.post-max-size
// http://php.net/manual/en/info.configuration.php#ini.max-input-time


// PHP 5.1+  SEE http://php.net/manual/en/function.date-default-timezone-set.php
date_default_timezone_set('America/Chicago');

// ESTABLISH THE BIGGEST FILE SIZE WE CAN ACCEPT - ABOUT 8 MB
$max_file_size = '8000000';

// ESTABLISH THE KINDS OF FILE EXTENSIONS WE WANT (USE UPPERCASE ONLY)
$file_exts = array
( 'XLS'
, 'XLSX'
, 'PDF'
, 'JPG'
)
;
$f_exts = implode(', ', $file_exts);

// ESTABLISH THE NAME OF THE DESTINATION FOLDER
$my_dir = getcwd();

// OR USE THIS TO PUT UPLOADS IN A SEPARATE FOLDER
$my_dir = 'throwaway';
if (!is_dir($my_dir))
{
    mkdir($my_dir);
}

// LIST OF THE ERRORS THAT MAY BE REPORTED IN $_FILES[]["error"] (THERE IS NO #5)
$errors = array
( 0 => "Success!"
, 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini"
, 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"
, 3 => "The uploaded file was only partially uploaded"
, 4 => "No file was uploaded"
, 5 => "UNDEFINED ERROR"
, 6 => "Missing a temporary folder"
, 7 => "Cannot write file to disk"
)
;


// IF THERE IS INFORMATION POSTED
if (!empty($_POST))
{
    // IF THERE ARE ERRORS
    $error_code    = $_FILES["userfile"]["error"];
    if ($error_code)
    {
        trigger_error($errors[$error_code], E_USER_ERROR);
    }

    // GET THE FILE SIZE
    $fsize = number_format($_FILES["userfile"]["size"]);

    // SYNTHESIZE THE NEW FILE NAME FOR TEMPORARY STORAGE
    $fname = basename($_FILES['userfile']['name']);

    // FAIL IF THIS IS NOT AN ALLOWABLE EXTENSION
    $f_ext = explode('.', $fname);
    $f_ext = end($f_ext);
    $f_ext = strtoupper(trim($f_ext));
    if (!in_array($f_ext, $file_exts)) trigger_error("$f_ext NOT ALLOWED.  CHOOSE FROM $f_exts", E_USER_ERROR);

    // THE SERVER PATH TO THE FILE
    $my_path
    = getcwd()
    . DIRECTORY_SEPARATOR
    . $my_dir
    . DIRECTORY_SEPARATOR
    . $fname
    ;

    // THE URL PATH TO THE FILE
    $my_url
    = $my_dir
    . DIRECTORY_SEPARATOR
    . $fname
    ;

    // MESSAGES ABOUT THE UPLOAD STATUS, IF ANY
    $msg = NULL;

    // IF THE FILE IS NEW (DOES NOT EXIST)
    if (!file_exists($my_path))
    {
        // IF THE MOVE FUNCTION WORKED CORRECTLY
        if (move_uploaded_file($_FILES['userfile']['tmp_name'], $my_path))
        {
            $upload_success = 1;
        }
        // IF THE MOVE FUNCTION FAILED IT PROBABLY THREW A MESSAGE
        else
        {
            $upload_success = -1;
            trigger_error("MOVE TO $my_path FAILED", E_USER_ERROR);
        }
    }

    // IF THE FILE ALREADY EXISTS
    else
    {
        $msg .= "<br/><b><i>$my_url</i></b> already exists" . PHP_EOL;

        // SHOULD WE OVERWRITE THE FILE? IF NOT
        if (empty($_POST["overwrite"]))
        {
            $upload_success = 0;
        }

        // IF WE SHOULD OVERWRITE THE FILE, TRY TO MAKE A BACKUP
        else
        {
            $now    = date('Y-m-d-His');
            $my_bak = $my_path . '.' . $now . '.bak';
            if (!copy($my_path, $my_bak))
            {
                $msg .= "<br/><strong>Attempted Backup Failed!</strong>" . PHP_EOL;
            }
            if (move_uploaded_file($_FILES['userfile']['tmp_name'], $my_path))
            {
                $upload_success = 2;
            }
            else
            {
                $upload_success = -1;
                trigger_error("MOVE TO $my_path FAILED", E_USER_ERROR);
            }
        }
    }

    // PREPARE A REPORT OF THE SCRIPT'S SUCCESS OR FAILURE
    if ($upload_success == 2) { $msg .= "<br/>A backup was made and the file was overwritten" . PHP_EOL; }
    if ($upload_success == 1) { $msg .= "<br/><strong>$my_url</strong> has been saved" . PHP_EOL; }
    if ($upload_success == 0) { $msg .= "<br/><strong>It was NOT overwritten.</strong>" . PHP_EOL; }
    if ($upload_success < 0)  { $msg .= "<br/><strong>ERROR: $my_url NOT SAVED - SEE WARNING FROM move_uploaded_file()</strong>" . PHP_EOL; }

    // ADD FILE SIZE AND PERMISSION INFORMATION
    if ($upload_success > 0)
    {
        $msg .= "<br/>$fsize bytes uploaded" . PHP_EOL;
        if (!chmod ($my_path, 0755))
        {
            $msg .= "<br/>chmod(0755) FAILED: fileperms() = ";
            $msg .= substr(sprintf('%o', fileperms($my_path)), -4);
        }
    }

    // SHOW THE SUCCESS OR FAILURE
    echo $msg;

    // SHOW A LINK TO THE FILE
    echo '<br/>'
    . '<a href="'
    . $my_url
    . '" target="_blank">'
    . "See: $my_url"
    . '</a>'
    ;
}


// CREATE THE FORM FOR INPUT (USING HEREDOC SYNTAX)
$form = <<<EOF
<h2>Upload a File</h2>
<p>
<form enctype="multipart/form-data" method="post">
<!--
    SOME IMPORTANT THINGS TO NOTE ABOUT THIS FORM...
    ENCTYPE= ATTRIBUTE IN THE HTML <FORM> TAG
    MAX_FILE_SIZE HIDDEN CONTROL MUST PRECEDE THE FILE INPUT CONTROLS
    INPUT NAME= IN TYPE=FILE DETERMINES THE NAME YOU FIND IN _FILES ARRAY
    ABSENCE OF ACTION= ATTRIBUTE IN <FORM> TAG CAUSES POST TO SAME URL
-->
<input type="hidden" name="MAX_FILE_SIZE" value="$max_file_size" />
Find a file to Upload ($f_exts): <input name="userfile" type="file" />
<br/>Check this box
<input autocomplete="off" type="checkbox" name="overwrite" /> to <strong>overwrite</strong> existing files
</br><input type="submit" value="Upload" />
</form>
</p>
EOF;

echo $form;

Open in new window

0
 

Author Comment

by:Mark
ID: 40221532
No, I just want an example of using file_get_contents with a multipart post .. Anotherwords, I need an example of uploading an image using file_get_contents. When I submit it via PHP, it should mimic the headers which I've posted above with exception of the image contents
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40221535
it is  a tutorial site for php upload file with POST at w3schools
http://www.w3schools.com/php/php_file_upload.asp

Review it  and then continue the question

Duncan
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40221538
Please see http://php.net/manual/en/function.file-get-contents.php

This function's not used to upload images.  It's used to make a GET-method request for a file (or similar resource) and read the file into a PHP variable.

Can you please tell us a little more about the objective in plain business (non-technical) terms?  If we can recognize the design pattern we can probably lead you to a solution.
0
 

Author Comment

by:Mark
ID: 40221539
Ok, let me clarify. I need an answer like this but with the correct parameters to upload an image from say a file called image.jpeg stored locally.

            $context_options = array (
                        'http' => array (
                              'method' => 'POST',
                              'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
                                    . "Content-Length: " . strlen($content) . "\r\n"
                                    . "Cookie: " . $cookies."\r\n",
                              'content' => $content
                              )
                        );

            $context = stream_context_create($context_options);            

            echo file_get_contents($get, false, $context);
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:Mark
ID: 40221544
I've been using file_get_contents for everything including POST and GET requests... I just want an example as the binary content of the image is tricky to me
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40221547
0
 
LVL 13

Assisted Solution

by:duncanb7
duncanb7 earned 334 total points
ID: 40221555
Image file format, it should be this
 'header'=> "Content-type: image/jpeg"
                                    . "Content-Length: " . strlen($content) . "\r\n"
                                    . "Cookie: " . $cookies."\r\n", 'content' => $content
                              )

Duncan
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 40221558
@duncanb7: From the original post: Content-Type: image/png
0
 
LVL 13

Accepted Solution

by:
duncanb7 earned 334 total points
ID: 40221561
Dear Ray,
I am not sure right  not from the original post is and later on  author mentionedd image/jpeg
Content-Type: image/png\r\n

So author will clarify it
Ray,Please advise kindly


Duncan
0
 

Author Comment

by:Mark
ID: 40221638
I figured it out but thanks for everybody's help
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now