8 Replies - 311 Views - Last Post: 10 July 2013 - 09:34 PM Rate Topic: -----

#1 poliosynthesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-June 13

Annoying error + still having problems with updating profile pictures

Posted 07 July 2013 - 11:52 PM

Hi guys,
you are probably getting really annoyed at the amateur mistakes I'm making, but I need your help again.
So, I got the file delete system working, however the code only changes the image when the name is changed. Also I got the system working when the name was changed, however I tried to remove all the errors that spring up when you change the name (and therefore the name of the folder where the picture is held) by saying that if '$mypic ==""' then closedir. This didn't work because I didn't know how to transfer variables between PHP pages. But after I got rid of that code, it stopped changing the profile pictures. It either showed the same one as before or none at all. Although before when is had been "working" if you didn't change the name the pictures would queue up next to each other. Any thoughts?

"change.php"
<?php
session_start();

if(isset($_POST['submit'])) {



$mypic = $_FILES['newupload']['name'] = $_POST[$mypic];
$temp = $_FILES['newupload']['tmp_name'];
$type = $_FILES['newupload']['type'];

$id = $_REQUEST['id'];
$newname = $_REQUEST ['newname'];
$newemail = $_REQUEST ['newemail'];
$newpass = ($_REQUEST ['newpass']);


if($newname && $newemail && $newpass) {

if(preg_match('/^([*+!.&#$¦\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i', $newemail)) {


if((strlen($newpass)>5) {
mysql_connect("localhost","root","") or die ("Connection error");
mysql_select_db ("testsite");
mysql_query("UPDATE users SET Name = '$newname' , Email = '$newemail' WHERE id='$id'");
$md5 = md5($newpass);
mysql_query("UPDATE users SET Password='$md5' WHERE id='$id'");



if(($type=="image/jpg")||($type=="image/jpeg")||($type=="image/png")||($type=="image/bmp")) {


$dir = "profiles/".$_SESSION['name']."/images";
$files = 0;
$handle = opendir($dir);
 while(($file = readdir($handle)) != FALSE ) {
 
     if($file !="."&& $file !=".."&& $file!="Thumbs.db"){ 
 unlink($dir."/".$file);
 $files++;

 
 }


}
closedir($handle);
sleep(1);
rename("profiles/".$_SESSION['name']."","profiles/$newname");
move_uploaded_file($temp, "profiles/$newname/images/$mypic");
echo "Your Details have been updated successfully";
header("Refresh:2; url=logout.php");



}else{
echo"That file type isn't accepted";
echo"<br />";}

}else {

echo"The password needs to be longer than five characters";
}




include('links.php');

}else{
echo"Please enter a valid email";

}
}else {

echo"Please complete the form";

}
}else{
echo"Access Denied";}







?>


This post has been edited by Dormilich: 08 July 2013 - 12:34 AM
Reason for edit:: fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Annoying error + still having problems with updating profile pictures

#2 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: Annoying error + still having problems with updating profile pictures

Posted 08 July 2013 - 02:22 AM

I'm a bit confused of what you are trying to do... I don't know what you already did, because I did not read previous posts of you. Could you please explain further what you are trying to do and what kind of errors appear?

In addition: DO NOT USE this mysql_nonsense()! use PDO's instead.
Was This Post Helpful? 0
  • +
  • -

#3 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Annoying error + still having problems with updating profile pictures

Posted 08 July 2013 - 02:37 PM

Here's the code properly indented for those interested:
<?php
session_start();

if(isset($_POST['submit'])) {

	$mypic = $_FILES['newupload']['name'] = $_POST[$mypic];
	$temp = $_FILES['newupload']['tmp_name'];
	$type = $_FILES['newupload']['type'];

	$id = $_REQUEST['id'];
	$newname = $_REQUEST['newname'];
	$newemail = $_REQUEST['newemail'];
	$newpass = ($_REQUEST['newpass']);

	if($newname && $newemail && $newpass) {

		if(preg_match('/^([*+!.&#$\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i', $newemail)) {

			if((strlen($newpass)>5) {
				mysql_connect("localhost","root","") or die ("Connection error");
				mysql_select_db ("testsite");
				mysql_query("UPDATE users SET Name = '$newname' , Email = '$newemail' WHERE id='$id'");
				$md5 = md5($newpass);
				mysql_query("UPDATE users SET Password='$md5' WHERE id='$id'");

				if(($type=="image/jpg")||($type=="image/jpeg")||($type=="image/png")||($type=="image/bmp")) {
				
					$dir = "profiles/".$_SESSION['name']."/images";
					$files = 0;
					$handle = opendir($dir);
					while(($file = readdir($handle)) != FALSE ) {
						if($file !="."&& $file !=".."&& $file!="Thumbs.db"){ 
							unlink($dir."/".$file);
							$files++;
						}
					}
					closedir($handle);
					sleep(1);
					rename("profiles/".$_SESSION['name']."","profiles/$newname");
					move_uploaded_file($temp, "profiles/$newname/images/$mypic");
					echo "Your Details have been updated successfully";
					header("Refresh:2; url=logout.php");

				} else {
					echo"That file type isn't accepted";
					echo"<br />";
				}
			} else {
				echo "The password needs to be longer than five characters";
			}
			include('links.php');
		} else {
			echo "Please enter a valid email";
		}
	} else {
		echo "Please complete the form";
	}
} else {
	echo "Access Denied";
}

?>


Was This Post Helpful? 1
  • +
  • -

#4 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Annoying error + still having problems with updating profile pictures

Posted 08 July 2013 - 03:00 PM

Alrighty, here's a couple of lessons in coding.

Firstly, make sure you use a consistent indentation standard, like I have done above. That will make life a lot easier when it comes to reading your own code, or when others need to read it. Tidiness (and commenting) in code are often overlooked, and are often the cause of a lot of headaches.

Secondly, if you have a bunch of validations you need to run (this is generally called design-by-contract), list out the validations indiviaully instead of nesting umpteen if..else statements. This also makes code easier to read.

I have applied these philosophies to your code and come up with this:
<?php
session_start();

// Prep you variables
$mypic = $_FILES['newupload']['name'] = $_POST[$mypic];
$temp = $_FILES['newupload']['tmp_name'];
$type = $_FILES['newupload']['type'];

$id = $_REQUEST['id'];
$newname = $_REQUEST['newname'];
$newemail = $_REQUEST['newemail'];
$newpass = ($_REQUEST['newpass']);

// List your invalidations individually
// Knockout invalidators
if(!isset($_POST['submit'])) {
	echo 'Access Denied';
	exit();
}

if(!$newname || !$newemail || !$newpass)) {
	echo 'Please complete the form';
	exit();
}

if(!preg_match('/^([*+!.&#$\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i', $newemail)) {
	echo 'Please enter a valid email';
	exit();
}

$invalid = false;
// Technial invalidators
if((strlen($newpass)<=5) {
	echo 'The password needs to be longer than five characters';
	$invalid = true;
}

if($type !== "image/jpg" && $type !== "image/jpeg" && $type !== "image/png" && $type !== "image/bmp") {
	echo "That file type isn't accepted";
	$invalid = true;
}


// Process the form
if(!$invalid)
{
	mysql_connect("localhost","root","") or die ("Connection error");
	mysql_select_db ("testsite");
	mysql_query("UPDATE users SET Name = '$newname' , Email = '$newemail' WHERE id='$id'");
	$md5 = md5($newpass);
	mysql_query("UPDATE users SET Password='$md5' WHERE id='$id'");

	$dir = "profiles/".$_SESSION['name']."/images";
	$files = 0;
	$handle = opendir($dir);
	while(($file = readdir($handle)) != FALSE ) {
		if($file !="."&& $file !=".."&& $file!="Thumbs.db"){ 
			unlink($dir."/".$file);
			$files++;
		}
	}
	closedir($handle);
	sleep(1);
	rename("profiles/".$_SESSION['name']."","profiles/$newname");
	move_uploaded_file($temp, "profiles/$newname/images/$mypic");
	echo "Your Details have been updated successfully";
	header("Refresh:2; url=logout.php");
}

include('links.php');

?>



That is how I would write the code, you may prefer your method though.

When you have this error, does the line "Your Details have been updated successfully" get echoed to the screen or not?
Was This Post Helpful? 0
  • +
  • -

#5 poliosynthesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-June 13

Re: Annoying error + still having problems with updating profile pictures

Posted 09 July 2013 - 01:58 AM

Yes, "Your details have been updated successfully" is echoed on the screen, but only until the page refreshes and takes you to the login page
thanks :D

I have got to say your way of coding is far more easy to read than mine :P
Was This Post Helpful? 0
  • +
  • -

#6 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Annoying error + still having problems with updating profile pictures

Posted 09 July 2013 - 03:04 AM

Alright, my next question is, are you sure the directory you are trying to write the new file to exists? By that I mean the directory profiles/$newname/images/$mypic. You may need to add the following code somewhere around line 53-55 in my code directly above. I have commented this code snippet heavily - read the comments and come to an understanding of what the code does, especially around the chmod stuff:
// Check if the directory exists
if(!is_dir("profiles/$newname/images/$mypic"))
{
	// If not, we make the directory
	mkdir(
		// 1st parameter - The directory name
		"profiles/$newname/images/$mypic",
		// 2nd parameter - chmod value for the directory (see http://www.onlineconversion.com/html_chmod_calculator.htm) 0644 gives read access to everyone and write access to Owner
		0644,
		// 3rd parameter - Boolean value determining whether the missing directories are created recursively
		true
	);
}


Was This Post Helpful? 0
  • +
  • -

#7 poliosynthesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-June 13

Re: Annoying error + still having problems with updating profile pictures

Posted 09 July 2013 - 04:30 AM

Hi,
I am almost totally sure that the directory exists. But will not be able to check until I am back on my other computer which is likely to be tomorrow (AWST). Thankyou for all your help and I will post if I have made any progress. Also one last question, you wouldn't be able to recommend a PHP book for a beginner would you?

Thanks so much! :D
Was This Post Helpful? 0
  • +
  • -

#8 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Annoying error + still having problems with updating profile pictures

Posted 09 July 2013 - 05:27 AM

Murach's books are excellent, though I haven't read the PHP one.
Was This Post Helpful? 0
  • +
  • -

#9 poliosynthesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-June 13

Re: Annoying error + still having problems with updating profile pictures

Posted 10 July 2013 - 09:34 PM

Hi,
thanks very much for all your help. It worked! You are awesome!
I had to slightly modify the code and fix some errors on my part the one major modification I made was the changing of.
if(!isdir("proifles/$newname/images/$mypic")) {


I changed the to
if(!$newname==$_SESSION['name']) {




Thankyou very much for your help :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1