Jump to content

[solved] PHP File Upload problem


lambik

Recommended Posts

Hi! This time I am working on file upload for my website (profile images). And what I want is to rename the uploaded file to username, so if user uploads file named image.jpg it will rename itself to username.jpg. So far I have this working code but I cant solve how to rename it and store to folder.

<?php$ftp_server = "********";$ftp_user_name = "********";$ftp_user_pass = "******";header('Content-Type: text/html;charset=UTF-8');		$spojenie=mysql_connect("*****", "******", "******");		if (!$spojenie) 			{			die("Chyba pri pripájaní k databáze.");			}		$db=mysql_select_db("******",$spojenie);		if (!$db) 			{			die("Chyba pri výbere databázy.");			}// set up a connection or die$conn_id = ftp_connect($ftp_server) or die("Nemožno pripojiť k $ftp_server"); $text =$_REQUEST['meno'];$dir = $text;// set up basic connection$conn_id = ftp_connect($ftp_server);// login with username and password$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);// try to create the directory $dirif (@ftp_mkdir($conn_id, $dir)) {   $destination_path = getcwd().DIRECTORY_SEPARATOR."".$_REQUEST['meno']."".DIRECTORY_SEPARATOR;   $result = 0;   $target_path = $destination_path . basename($_FILES['myfile']['name']);   if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path))   {	  $result = 1;   }	    sleep(1);   echo("   <script language=\"javascript\" type=\"text/javascript\">window.top.window.stopUpload(<?php echo $result; ?>);</script>   ");}else{	$destination_path = getcwd().DIRECTORY_SEPARATOR."".$_REQUEST['meno']."".DIRECTORY_SEPARATOR;   $result = 0;   $target_path = $destination_path . basename($_FILES['myfile']['name']);   if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path))   {	  $result = 1;   }	    sleep(1);   echo("   <script language=\"javascript\" type=\"text/javascript\">window.top.window.stopUpload(<?php echo $result; ?>);</script>   ");}// close the connectionftp_close($conn_id);?>

I will appreciate any help. Thanks

Link to comment
Share on other sites

Why don't you just give it a different name when you use move_uploaded_file?
Because I don't understand which part of this block should I edit and where do I have to put $_SESSION[user] (that is the name i want for it but it must also have original extension). Can you describe it please little bit more?
Link to comment
Share on other sites

This is the documentation for move_uploaded_file:http://www.php.net/manual/en/function.move-uploaded-file.phpAs you can see, the second parameter is the filename where you want to move the file to, including the path (the first parameter is the filename of the temp uploaded file). So instead of using $_FILES['myfile']['name'], you can substitute another name there. If you want to get the original file extension, you can do it like this:$ext = @array_pop(explode('.', $_FILES['myfile']['name']));

Link to comment
Share on other sites

Thanks for help, although it is still not working as i expected i can see my progress now so i will try to handle this. ThanksEDIT: I stored it as a jpg but now i only need to give it username.EDIT2: Buala, its working perfectly now, i really appreciate your help. :)

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...