Jump to content

[SOLVED] Upload file!


runnerjp

Recommended Posts

Ok i have a form..

<form name="form1" method="post" action="">

  <p>Name:
    <input name="Name" type="text" maxlength="20"> 
    <br />
    <br />
Email:
<input name="Name" type="text" maxlength="20">
<br />
<br />
Event: 
<input name="Name" type="text" maxlength="20">
<br />
<br />
Venue: 
<input name="Name" type="text" maxlength="20">
</p>
  <p> </p>
  <p>
    <label>
    <input type="submit" name="submit" id="submit" value="Submit">
    </label>
    <br />
    <br />
    </p>

 

I want to have the option for users to upload a file (its an entry form to a compertition) but i wanted to know if i could add it  to the current form so the same submit button is used. But i also want the form to be optional so if the dont have a file to upload they can still enter the data. Is this possible and if so how?

Link to comment
https://forums.phpfreaks.com/topic/180838-solved-upload-file/
Share on other sites

Hi runnerjp,

 

Have a look at this tutorial which will take you through the entire process.

 

Also, on the code you have posted, all of your fields have the same name (e.g. name="Name"), this is going to cause you problems so make sure each field is named individually (e.g. name="Name", name="Event", "name="Email" etc)

 

Hope this helps.

Link to comment
https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954015
Share on other sites

Ok followed it all and for some reason it wont let me upload a .doc saying invalid file

 

here is my script

 

<?php
if ((($_FILES["file"]["type"] == "text/doc"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("include/entry" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "include/entry" . $_FILES["file"]["type"];
      }
    }
  }
else
  {
  echo "Invalid file".$_FILES["file"];
  }

print_r($_REQUEST['form']);

?>

and my output is

 

Invalid fileArray

(

    [name] => test

    [file] => cover.doc

)

 

Link to comment
https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954027
Share on other sites

here is html...

 

<!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="/include/addfixture.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>

 

 

also here is the javascript just incase..

 

/**
*
*  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);
	}
}

}

Link to comment
https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954043
Share on other sites

Ok i have even made it simple

 

<?php
$target_path = "/include/entrys/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

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

//print_r($_REQUEST['form']);

?>

 

and i still get There was an error uploading the file, please try again!

Link to comment
https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954184
Share on other sites

Ok i added it ...

 

<form action="/include/addfixture.php" method="post" onsubmit="return AIM.submit(this, {'onStart' : startCallback, 'onComplete' : completeCallback})"enctype="multipart/form-data">
      <div><label>Name:</label> <input type="text" name="form[name]" /></div>
      <div><label>File:</label> <input type="file" name="form" /></div>
      <div><input type="submit" value="SUBMIT" /></div>
   </form>

  but im still getting invalid file

Link to comment
https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954196
Share on other sites

For debugging, add the following code immediately after your first opening <?php tag in your form processing code so that you can see exactly what you are getting from the form and what errors might be occurring -

ini_set("display_errors", "1");
error_reporting(E_ALL);
echo "<pre>";
echo "POST:";
print_r($_POST);
echo "FILES:";
print_r($_FILES);
echo "</pre>";

Link to comment
https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954202
Share on other sites

Ok over that yet still no upload..here out ouput

POST:Array

(

    [form] => Array

        (

            [name] => test

        )

 

)

FILES:Array

(

    [file] => Array

        (

            [name] => ntu app.doc

            [type] => application/msword

            [tmp_name] => /tmp/phpvuQQNO

            [error] => 0

            => 339968

        )

 

)

 

Invalid fileArrayArray

(

    [name] => test

)

 

 

and here is code

 

<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
echo "<pre>";
echo "POST:";
print_r($_POST);
echo "FILES:";
print_r($_FILES);
echo "</pre>";
if ((($_FILES["file"]["type"] == "application/msword
"))
&& ($_FILES["file"]["size"] < 500000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("include/entry/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "include/entry/" . $_FILES["file"]["type"];
      }
    }
  }
else
  {
  echo "Invalid file".$_FILES["file"];
  }

print_r($_REQUEST['form']);




Link to comment
https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954230
Share on other sites

The "Invalid file" message implies that the following statemet is FALSE -

if ((($_FILES["file"]["type"] == "application/msword
"))
&& ($_FILES["file"]["size"] < 500000))

 

Since the ["size"] of the actual file is clearly < 500000, that would mean that the ["type"] of the actual file does not match the value in the conditional test.

 

The value in the conditional test is "application/msword\n" and the value form the uploaded file is "application/msword". Those are not the same and an == comparison between them will be false.

 

It matters if there is an extra \n on the end of a value that you copy/paste into code.

Link to comment
https://forums.phpfreaks.com/topic/180838-solved-upload-file/#findComment-954323
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.