-1
$image = $_FILES['picture']['name'];
$id=$_SESSION['id'];
//This function separates the extension from the rest of the file name and returns it 
 function findexts ($filename) 
 { 
 $filename = strtolower($filename) ; 
 $exts = split("[/\\.]", $filename) ; 
 $n = count($exts)-1; 
 $exts = $exts[$n]; 
 return $exts; 
 }  

 $ext = findexts ($_FILES['picture']['name']) ; 
 //This assigns the subdirectory you want to save into... make sure it exists!
 $target = "mainimage/";
 //This combines the directory, the random file name, and the extension
 $target = $target . $id.".".$ext; 
 if(move_uploaded_file($_FILES['picture']['tmp_name'], $target)) 
 {
 echo ("This is your new profile picture!");
 } 
 else
 {
 echo "Sorry, there was a problem uploading your file.";
 }

for some reason the "else" keeps showing up (there was a problem uploading the file). I put comments in everything that I am trying to do. please help! thanks!

user3370114
  • 5
  • 1
  • 7
  • have you verified the file is being uploaded in the first place? –  Apr 30 '14 at 22:51
  • Are you sure that your server has write access to the directory you are trying to move the file? – Manolis Agkopian Apr 30 '14 at 22:53
  • Is `session_start();` loaded? If it isn't, then that's one reason why your code is failing. – Funk Forty Niner Apr 30 '14 at 23:08
  • Yes i started the session and the server has access to write in the directory – user3370114 Apr 30 '14 at 23:13
  • 1
    What does `var_dump($id);` or `var_dump($_SESSION['id']);` and also `var_dump($target);` give you? @user3370114 placing `var_dump($target);` after `$target = $target . $id.".".$ext;` – Funk Forty Niner Apr 30 '14 at 23:44
  • @Fred-ii- this is my result: string(1) "2" string(1) "2" string(15) "mainimage/2.jpg" – user3370114 May 01 '14 at 00:03
  • So, the id is indeed `2`? – Funk Forty Niner May 01 '14 at 00:06
  • @Fred-ii- yes (random letters to make post long enough) – user3370114 May 01 '14 at 00:15
  • I honestly don't know what it could be at this point, *baffled*. – Funk Forty Niner May 01 '14 at 00:15
  • Plus, your form has `enctype="multipart/form-data"`? including POST method and not GET? – Funk Forty Niner May 01 '14 at 00:16
  • that is the form right there ^ thank you anyway! – user3370114 May 01 '14 at 00:25
  • Ok. Well while I was waiting, I tested your code and it worked for me. My last ditch effort question is, does your form contain `` having `name="picture"`? Plus, are you running this on your own machine or hosted site? – Funk Forty Niner May 01 '14 at 00:26
  • Add Photo:
    that is the line
    – user3370114 May 01 '14 at 00:28
  • oh and i am running this locally – user3370114 May 01 '14 at 00:30
  • I don't know what it could be then. Are you running the entire script from the root of your system? Because if you're outside the root and inside a sub-folder with having the `mainimage` folder as a sub-folder from where you're running the script, then that will fail. – Funk Forty Niner May 01 '14 at 00:34
  • The mainimage/ directory is in my htdocs folder which is where i run all of my php files from. The only thing that i can think of is that the form is havint trouble getting the file information. Could that be something? – user3370114 May 01 '14 at 00:37
  • `root->form/script -> mainimage/` right? Or is your script inside the `mainimage` folder? As for the form getting the file information, I doubt it, since you've shown me a `var_dump();` and shows that the information is getting through. Another thing would be to check what your upload max size is. By default, PHP I think sets it to 2mb. If you're trying to upload a file bigger than that, then it will contribute to the problem. – Funk Forty Niner May 01 '14 at 00:41
  • the file is 3.46mb! is there a way that i can say that i want to upload a bigger file than 2? – user3370114 May 01 '14 at 00:49
  • Yes, see this Q&A on SO => http://stackoverflow.com/q/2184513/ plus you may have to do it through `.htaccess` if you have a hard time with `php.ini` file. However, try it with a smaller file and see what happens. – Funk Forty Niner May 01 '14 at 01:31
  • 1
    Thank you very much for your help tonight. Im glad to know that there are still good people in the world – user3370114 May 01 '14 at 01:37
  • You're very much welcome. Have you gotten any positive results so far? – Funk Forty Niner May 01 '14 at 01:38
  • no :( i dont have permission to change the ini file even though it is my computer which is pretty annoying – user3370114 May 01 '14 at 01:46
  • but, i was able to upload a picture that was only about 200kb so at least we know that it is definitely a size issue – user3370114 May 01 '14 at 01:47
  • Try adding this to your `.htaccess` file (the first character is a dot) then if you can't modify your `php.ini` file. If you don't have one, create it and put it in your root `php_value upload_max_filesize 20M php_value post_max_size 30M` <= in 2 lines, then restart Apache/PHP and maybe even `php_value memory_limit 128M` – Funk Forty Niner May 01 '14 at 01:59
  • ugh this is still not working :( what about using an ini_set()? – user3370114 May 01 '14 at 02:11
  • Apparently that won't work => http://stackoverflow.com/a/3263496/ – Funk Forty Niner May 01 '14 at 02:14
  • is there just a way to change the permission for the php.ini file? this is my own personal laptop so im not sure why i wouldnt have access – user3370114 May 01 '14 at 02:19
  • Hard to say. Try doing it the same way you would in the file manager or right-click on the file then click on "properties" and maybe if there's an "advanced" tab. Assuming you're on Windows. – Funk Forty Niner May 01 '14 at 02:25
  • everything is all good! thank you so much for your help tonight. im just curious, what do you do that you know all of this stuff? – user3370114 May 01 '14 at 02:37
  • You're quite welcome. Well my friend, what I do is just taken from past experience and from reading other people's questions and answers here on StackOverflow, plus working with computers since the early 1990's helps ;-) If my comments helped to resolve this, let me know if you would like me to use some of them in an answer, so we can now close it properly. All the best, cheers! – Funk Forty Niner May 01 '14 at 14:47
  • Yes i suppose that we can resolve this – user3370114 May 01 '14 at 19:46
  • It has been done. Glad to have helped, cheers. @user3370114 – Funk Forty Niner May 01 '14 at 23:35

1 Answers1

1

To close the question as per OP's request.

The issue is that you need to increase the maximum size for uploads. PHP's default is usually 2M and you are most probably trying to upload a file larger than what the maximum setting is set to.

This can be achieved in two ways.

By editing your php.ini file with the following settings:

; Maximum allowed size for uploaded files.
upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize
post_max_size = 40M

or via an .htaccess file placed in the root of your server:

php_value upload_max_filesize 20M
php_value post_max_size 30M
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141