Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts

    AJAX image upload

    Hi all. Been a long time since ive done this and i know its something simple.

    Using this script:
    http://www.webtoolkit.info/demo/java.../aim/demo.html

    it outputs an array which i then need to add an upload script to for it to work.
    currently using this:

    PHP Code:
    <?php

    $filename 
    $_REQUEST['form']['name'].".jpg";
    $image_location $filename;

    $uploadedfile $_REQUEST['form']['file'];
    $src imagecreatefromjpeg($uploadedfile);
    list(
    $width,$height)=getimagesize($uploadedfile);

    $newwidth=200;
    $newheight=134;
    $tmp=imagecreatetruecolor($newwidth,$newheight);

    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

    imagejpeg($tmp,$filename,100);
    imagedestroy($src);
    imagedestroy($tmp); 
     
    ?>
    But getting the following errors:

    Warning: imagecreatefromjpeg(Block paving.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in C:\wamp\www\toolBar\index2.php on line 7



    Warning: getimagesize(Block paving.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\wamp\www\toolBar\index2.php on line 8



    Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\wamp\www\toolBar\index2.php on line 14



    Warning: imagedestroy() expects parameter 1 to be resource, boolean given in C:\wamp\www\toolBar\index2.php on line 17

    any help much appreciated.
    Last edited by Phil Jackson; 10-29-2009 at 05:43 AM.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    PHP Code:
    $filename $_REQUEST['form']['name'].".jpg"
    That is the filename of the file being uploaded but the file doesn't exist there. You have to use move_uploaded_file() to copy the file from its temporary holding place to somewhere on your file system.

    http://us.php.net/manual/en/features.file-upload.php

  • #3
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?php

    $target_path 
    "./";

    $target_path $target_path basename$_REQUEST['form']['file'] ); 

    if(
    move_uploaded_file($_REQUEST['form']['file'] , $target_path)) {
        echo 
    "The file ".  basename$_REQUEST['form']['file'] ). 
        
    " has been uploaded";
    } else{
        echo 
    "There was an error uploading the file ".$_REQUEST['form']['file'].", please try again!";
    }

    ?>
    doesn't work either
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Phil Jackson View Post
    PHP Code:
    <?php

    $target_path 
    "./";

    $target_path $target_path basename$_REQUEST['form']['file'] ); 

    if(
    move_uploaded_file($_REQUEST['form']['file'] , $target_path)) {
        echo 
    "The file ".  basename$_REQUEST['form']['file'] ). 
        
    " has been uploaded";
    } else{
        echo 
    "There was an error uploading the file ".$_REQUEST['form']['file'].", please try again!";
    }

    ?>
    doesn't work either
    PHP Code:
    print_r($_FILES); 
    best regards

  • #5
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    nor

    PHP Code:
    <?php

    $target_path 
    "./";

    $target_path $target_path basename$_REQUEST['form']['name'] ); 

    if(
    move_uploaded_file($_REQUEST['form']['name'] , $target_path)) {
        echo 
    "The file ".  basename$_REQUEST['form']['name'] ). 
        
    " has been uploaded";
    } else{
        echo 
    "There was an error uploading the file ".$_REQUEST['form']['name'].", please try again!";
    }

    ?>
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #6
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    returns empty array
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Throw the $_REQUEST array away. Use the $_FILES array. (oesxyl tried to give you a hint in that direction but was too subtle I guess )

  • Users who have thanked Fumigator for this post:

    oesxyl (10-27-2009)

  • #8
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    Quote Originally Posted by Phil Jackson View Post
    returns empty array
    no good
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Phil Jackson View Post
    no good
    PHP Code:
    print $_POST['file']; 
    is the file name and
    PHP Code:
    print $_POST['name']; 
    will be the name from form. You use form[name], form[file] as names for input, is not good, is like my dog name is dog,
    - $_REQUEST can be a GET or POST, mixing them is not a good idea.
    - you still don't check if file exists before doing something whit them,
    - $_FILES will store what you need, including error codes.

    best regards

  • #10
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Quote Originally Posted by Phil Jackson View Post
    no good
    It's likely you don't have the form set up correctly. You need an enctype on the tag; read the link I provided for all the information you need to get it working.

  • #11
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    	<title>Iframe Ajax</title>
    	<script type="text/javascript" src="webtoolkit.aim.js"></script>
    	<script type="text/javascript">
    		function startCallback() {
    			// make something useful before submit (onStart)
    			return true;
    		}
     
    		function completeCallback(response) {
    			// make something useful after (onComplete)
    			document.getElementById('nr').innerHTML = parseInt(document.getElementById('nr').innerHTML) + 1;
    			document.getElementById('r').innerHTML = response;
    		}
    	</script>
    </head>
     
    <body>
     
    	<form action="index2.php" method="post" onsubmit="return AIM.submit(this, {'onStart' : startCallback, 'onComplete' : completeCallback})">
    		<div><label>Name:</label> <input type="text" name="form[name]" /></div>
    		<div><label>File:</label> <input type="file" name="form[file]" /></div>
    		<div><input type="submit" value="SUBMIT" /></div>
    	</form>
     
    	<hr/>
     
    	<div># of submited forms: <span id="nr">0</span></div>
    	<div>last submit response (generated by form action - index.php file): <pre id="r"></pre></div>
     
    </body>
    </html>
    PHP Code:
    <?php

    $target_path 
    "./";

    $target_path $target_path basename$_REQUEST['form']['name'] ); 

    if(
    move_uploaded_file($_REQUEST['form']['name'] , $target_path)) {
        echo 
    "The file ".  basename$_REQUEST['form']['name'] ). 
        
    " has been uploaded";
    } else{
        echo 
    "There was an error uploading the file ".$_REQUEST['form']['name'].", please try again!";
    }

    ?>
    Code:
    // JavaScript Document
    /**
    *
    *  AJAX IFRAME METHOD (AIM)
    *  http://www.webtoolkit.info/
    *
    **/
     
    AIM = {
     
    	frame : function(c) {
     
    		var n = 'f' + Math.floor(Math.random() * 99999);
    		var d = document.createElement('DIV');
    		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
    		document.body.appendChild(d);
     
    		var i = document.getElementById(n);
    		if (c && typeof(c.onComplete) == 'function') {
    			i.onComplete = c.onComplete;
    		}
     
    		return n;
    	},
     
    	form : function(f, name) {
    		f.setAttribute('target', name);
    	},
     
    	submit : function(f, c) {
    		AIM.form(f, AIM.frame(c));
    		if (c && typeof(c.onStart) == 'function') {
    			return c.onStart();
    		} else {
    			return true;
    		}
    	},
     
    	loaded : function(id) {
    		var i = document.getElementById(id);
    		if (i.contentDocument) {
    			var d = i.contentDocument;
    		} else if (i.contentWindow) {
    			var d = i.contentWindow.document;
    		} else {
    			var d = window.frames[id].document;
    		}
    		if (d.location.href == "about:blank") {
    			return;
    		}
     
    		if (typeof(i.onComplete) == 'function') {
    			i.onComplete(d.body.innerHTML);
    		}
    	}
     
    }
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #12
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    could really do with the help, much appreciated
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #13
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Phil Jackson View Post
    could really do with the help, much appreciated
    Fumigator already said about enctype:

    Note: Be sure your file upload form has attribute enctype="multipart/form-data" otherwise the file upload will not work.
    http://us.php.net/manual/en/features...ost-method.php

    best regards

  • #14
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    do you have imagemagik or GD?

  • #15
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    I have tried with both but still not work
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •