Jump to content

Wont copy a file.


PGTibs

Recommended Posts

I have a webpage which, is meant to copy a photo to a directory and then put the file name into a database. However, everytime i try and run the page i just get...

 

Parse error: parse error in C:\xampp\htdocs\xampp\panel\admin\editphoto.php on line 46

 

Line 46:

if (copy('$name', '$dest')) {

 

Full Page:

<? session_start();

include '../config.php';
include '../online.php';

$ip = $_SERVER['REMOTE_ADDR']; //get the ip of the current user

if(!isset($_SESSION['session_username']) || empty($_SESSION['session_username']) || $ip!= $_SESSION['session_ip']) {

//if the username is not set or the session username is empty or the ip does not match the session ip log them out

session_unset(); //clears firefox

session_destroy(); //clears IE

echo "An Error Has Occured. Please log in again or contact your webmaster.";

exit; } ?>

<? if($session_level == "3") { echo "You are not of the required level to access this page."; } else { ?>



<table width="100%" background="../../images/header.PNG">
<tr><td>
<font face="Verdana" size="1"><b>Change Your Login Photo</b></font>
</td></tr></table>



<?
$action = $_GET["action"];
if($action == "change") {

$username = $_POST["username"];
$name = $_POST["name"];

if(!$username) { echo "<b>You must enter a photo!</b>"; exit; }

$name = ($name);

$username = ($username);

$dest = ("/avatarimages")

if (copy('$name', '$dest')) {

$query = mysql_query("UPDATE `staff` SET photo='$name' WHERE username='$username'") or die(mysql_error());

echo "<font face=verdana size=1><br><br>The photo has been edited.";

} else {

echo "<font face=verdana size=1><br><br>The photo failed to be edited.";



} else {

echo "

<p align=`centre`><form method=post action=\"editphoto.php?action=change\">

<table border=\"0\">

<tr><td><font size=\"1\" face=\"Verdana\">Username:<td><input name=\"username\"></td></tr>

<tr><td><font size=\"1\" face=\"Verdana\">New Avatar:<td><input type='file' name=\"name\"></td></tr>

<tr><td></td><td><input type=submit value=\"Edit Avatar\" accesskey=\"s\"></td></tr>

</table>

</form>"; }

?><? } ?>

Link to comment
Share on other sites

Ok so i did that, and then sorted out a few other problems....

 

Now ive got the same error but its saying on line 70 which is the last line...

 

<? session_start();

include '../config.php';
include '../online.php';

$ip = $_SERVER['REMOTE_ADDR']; //get the ip of the current user

if(!isset($_SESSION['session_username']) || empty($_SESSION['session_username']) || $ip!= $_SESSION['session_ip']) {

//if the username is not set or the session username is empty or the ip does not match the session ip log them out

session_unset(); //clears firefox

session_destroy(); //clears IE

echo "An Error Has Occured. Please log in again or contact your webmaster.";

exit; } ?>

<? if($session_level == "3") { echo "You are not of the required level to access this page."; } else { ?>



<table width="100%" background="../../images/header.PNG">
<tr><td>
<font face="Verdana" size="1"><b>Change Your Login Photo</b></font>
</td></tr></table>



<?
$action = $_GET["action"];
if($action == "change") {

$username = $_POST["username"];
$name = $_POST["name"];

if(!$username) { echo "<b>You must enter a photo!</b>"; exit; }

$name = ($name);

$username = ($username);

$dest = ("/avatarimages");

if (copy('$name', '$dest')) {

$query = mysql_query("UPDATE `staff` SET photo='$name' WHERE username='$username'") or die(mysql_error());

echo "<font face=verdana size=1><br><br>The photo has been edited.";

} else {

echo "

<p align=`centre`><form method=post action=\"editphoto.php?action=change\">

<table border=\"0\">

<tr><td><font size=\"1\" face=\"Verdana\">Username:<td><input name=\"username\"></td></tr>

<tr><td><font size=\"1\" face=\"Verdana\">New Avatar:<td><input type='file' name=\"name\"></td></tr>

<tr><td></td><td><input type=submit value=\"Edit Avatar\" accesskey=\"s\"></td></tr>

</table>

</form>"; }

} ?>

Link to comment
Share on other sites

Yikes! I hate escaped slashes in PHP/HTML. Try it this way:

 

else {
?>

<p align="centre"><form method="post" action="editphoto.php?" action="change">

<table border="0">

<tr><td><font size="1" face="Verdana">Username:<td><input name="username"></td></tr>

<tr><td><font size="1" face="Verdana">New Avatar:<td><input type="file" name="name"></td></tr>

<tr><td></td><td><input type="submit" value="Edit Avatar" accesskey="s"></td></tr>

</table>

</form>
<?php }

 

Which keeps all your HTML outside the PHP parser, and means that the rabbit ears and apostrophes aren't quite as confused.

Link to comment
Share on other sites

I'm surprised no one has caught it; you're using single quotes:

 

if (copy('$name', '$dest')) {

 

You're telling it the filenames are literally "$name" and "$dest"... not the values OF $name and $dest. Change those single quotes '...' to double quotes "..." or remove them entirely, you don't need them.

 

if (copy($name, $dest)) {

 

Variables in single quotes are -not- replaced. ie.

 

$test = 'hello';

// Prints out "$test, how are you"
echo '$test, how are you';

// Prints out "hello, how are you"
echo "$test, how are you";

Link to comment
Share on other sites

Well now i understand that better so thanks for that, however.

 

Same error, same line. Still not working?

 

When you post an error message, post it and also post the line that is causing the problems, then post your code under that.

 

Without it, no one wants to go through and count your lines to find which one is the problematic one.

Link to comment
Share on other sites

Oh, you have four if statements at the end of your script that follow this structure:

 

if {

    if {

      if {}

      if {}

    }

}

 

You have left out the closing brace for the topmost IF statement, like so:

 

if {

    if {

      if {}

      if {}

    }

 

More specifically, this IF statement was what hasn't been closed:

 

<? if($session_level == "3") { echo "You are not of the required level to access this page."; } else { ?>

 

You need to add another closing bracket at the end of the script.

Link to comment
Share on other sites

Scrap the idea of it working. So now, when i run the page it loads, but when i submit the form i get...

 

Change Your Login Photo

Warning: copy(image.png) [function.copy]: failed to open stream: No such file or directory in C:\xampp\htdocs\xampp\panel\admin\editphoto.php on line 46


The photo has been edited.

 

Anyone got an idea of why this isn't working?

Link to comment
Share on other sites

It says the file image.png doesn't exist, so it can't be copied.

 

Assuming the image does in fact exist, please give me the absolute path for:

a) your script

b) your image

 

PHP can be a little weird when it comes to dealing with relative paths in some cases.

 

EDIT:

Also, I also noticed:

 

$dest = '/avatarimages';

 

For the copy function, you need to specify a filename, not just the directory. For example, you want to copy image.png to /avatarimages/image.png -- then image.png needs to be part of $dest.

 

So you may want to change it to:

 

$dest = '/avatarimages/'.basename($name);

Link to comment
Share on other sites

Oooh, I completely missed the whole file upload thing. What you're doing wrong is you're trying to access the upload via $_POST['name']. However, File upload fields in forms are handled differently in PHP. They are put into a different superglobal solely for file uploads. The $_FILES superglobal.

 

Your file should appear in $_FILES['name'], and that will contain an array like so:

 

$_FILES['name']['name']  // The name of the file

$_FILES['name']['tmp_file']  // The path of the TEMPORARY file that is stored on the server

$_FILES['name']['type']  // the mime type of the file; for example "image/png"

$_FILES['name']['size']  // the size of the file

$_FILES['name']['error']  // error code if an error occurred

 

First off, you can't use copy() in this scenario, you have to use

 

move_uploaded_file($_FILES['name']['tmp_file'], '/avatarimages/'.$_FILES['name']['name']);

 

You can check whether an uploaded file is real with

 

is_uploaded_file($_FILES['name']['tmp_file']);

 

Hope that makes sense.

Link to comment
Share on other sites

Makes perfect sense, only, being a complete beginner to php, what lines do i have to remove to put those lines in? Just adding them in gives me a parse error on line 45...

 

line 45:

$_FILES['name']['tmp_file']  // The path of the TEMPORARY file that is stored on the server

Link to comment
Share on other sites

Hey,

 

Should look something like this: it's untested, but should work, what i did:

 

1) reformatted for readability on the forum

2) changed the name of the HTML file field to "avatar"

3) replaced copy() with move_uploaded_file()

4) replaced any echo/exit's with die's.

 

Tell me how it goes.

 

<?php

session_start();
include '../config.php';
include '../online.php';

$ip = $_SERVER['REMOTE_ADDR']; //get the ip of the current user
if(!isset($_SESSION['session_username']) || empty($_SESSION['session_username']) || $ip!= $_SESSION['session_ip']) 
{
	//if the username is not set or the session username is empty or the ip does not match the session ip log them out
	session_unset(); //clears firefox
	session_destroy(); //clears IE

	die("An Error Has Occured. Please log in again or contact your webmaster."); 
}

if($session_level == "3") 
	die("You are not of the required level to access this page.");

?>

<table width="100%" background="../../images/header.PNG">
<tr><td>
<font face="Verdana" size="1"><b>Change Your Login Photo</b></font>
</td></tr></table>

<?php

if(isset($_GET["action"]) && $_GET["action"] == "change") 
{

	$username = $_POST["username"];
	$file = $_FILES['avatar'];

	if(!$username) 
		die("<b>You must enter a photo!</b>");

	$dest = '/avatarimages/'.basename($file['name']);
	if( move_uploaded_file($file['tmp_file'], $dest) ) {

		$query = mysql_query("UPDATE `staff` SET photo='$name' WHERE username='$username'") or die(mysql_error());
		echo "<font face=verdana size=1><br><br>The photo has been edited.";

	} else {

		echo "
		<p align=`centre`><form method=post action=\"editphoto.php?action=change\">
		<table border=\"0\">
		<tr><td><font size=\"1\" face=\"Verdana\">Username:<td><input name=\"username\"></td></tr>
		<tr><td><font size=\"1\" face=\"Verdana\">New Avatar:<td><input type='file' name=\"avatar\"></td></tr>
		<tr><td></td><td><input type=submit value=\"Edit Avatar\" accesskey=\"s\"></td></tr>
		</table>
		</form>"; 

	}

} 

?>

Link to comment
Share on other sites

Oh! Here we go: (messed up the grouping of the if statements)

 

<?php

   session_start();
   include '../config.php';
   include '../online.php';

   $ip = $_SERVER['REMOTE_ADDR']; //get the ip of the current user
   if(!isset($_SESSION['session_username']) || empty($_SESSION['session_username']) || $ip!= $_SESSION['session_ip']) 
   {
      //if the username is not set or the session username is empty or the ip does not match the session ip log them out
      session_unset(); //clears firefox
      session_destroy(); //clears IE

      die("An Error Has Occured. Please log in again or contact your webmaster."); 
   }

   if($session_level == "3") 
      die("You are not of the required level to access this page.");

?>

<table width="100%" background="../../images/header.PNG">
<tr><td>
<font face="Verdana" size="1"><b>Change Your Login Photo</b></font>
</td></tr></table>

<?php

   $action = $_GET["action"];
   if( $action == "change" ) 
   {

      $username = $_POST["username"];
      $file = $_FILES['avatar'];

      if(!$username) 
         die("<b>You must enter a photo!</b>");
      
      $dest = '/avatarimages/'.basename($file['name']);
      if( move_uploaded_file($file['tmp_file'], $dest) ) {

         $query = mysql_query("UPDATE `staff` SET photo='$name' WHERE username='$username'") or die(mysql_error());
         echo "<font face=verdana size=1><br><br>The photo has been edited.";

      } else { die('Could not upload the image.'); }
  
} else {
         
 echo "
 <p align=`centre`><form method=post action=\"editphoto.php?action=change\">
 <table border=\"0\">
 <tr><td><font size=\"1\" face=\"Verdana\">Username:<td><input name=\"username\"></td></tr>
 <tr><td><font size=\"1\" face=\"Verdana\">New Avatar:<td><input type='file' name=\"avatar\"></td></tr>
 <tr><td></td><td><input type=submit value=\"Edit Avatar\" accesskey=\"s\"></td></tr>
 </table>
 </form>"; 
  
   }

?>

Link to comment
Share on other sites

Hmm, this is where diagnosing the issue gets a little murky; how big is the file and how long is it taking for it to upload, roughly. PHP sets a default maximum upload filesize at 2MB, and a maximum script execution time of 30 seconds.

 

If it takes longer than 30 seconds, or is larger than 2MB, then you'll need to find your host's PHP.INI file and edit it to reflect that. That file pretty much controls the PHP environment and is usually found in the root folder of your website; but that depends on your host. Some hosts don't allow you to modify your php.ini file, but usually they'll do it for you if you put in the request.

 

If you do have access, then you'll want to check to see if file uploads are enabled, the default maximum upload is more than 2MB, and max execution time is greater than 30 seconds. The lines you have to edit are below and are in the php.ini file; just do a text search and you'll find them.

 

...
max_execution_time = 30     ; Maximum execution time of each script, in seconds
...
; Whether to allow HTTP file uploads.
file_uploads = On
...
; Maximum allowed size for uploaded files (in MB)
upload_max_filesize = 2M
...

 

If you don't have access to your php.ini file, make a test.php file with this code in it:

 

<?php phpinfo(); ?>

 

It will display a page of information on php.ini; and with that we can investigate your host's default settings (they may have changed them from their true defaults). If you take this route, send the url to that test.php page to me via PM--you usually don't want to put that for just anyone to see.

Link to comment
Share on other sites

Oh right, running it locally >_< forgot. Oh, well it may be that you have an absolute path in $dest. Make it into a relative path.

 

Oh, and get rid of basename there, I realized you don't need it.

 

$dest = 'avatarimages/'.$file['name'];

 

And make sure there's an avatarimages folder where the script is, of course.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.