gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
php code for uploaded file?
- User
php code for uploaded file?
- Assistant
Here is an example of PHP code for handling an uploaded file:
<?php // Check if a file was uploaded if ($_FILES['file']['error'] === UPLOAD_ERR_OK) { // Set the destination directory $uploadDir = 'uploads/'; // Generate a unique filename $fileName = uniqid() . '_' . $_FILES['file']['name']; // Move the uploaded file to the destination directory if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadDir . $fileName)) { echo 'File is valid, and was successfully uploaded.'; } else { echo 'Error moving uploaded file.'; } } else { echo 'Error uploading file.'; } ?>Make sure that you have properly set the
enctype="multipart/form-data"attribute in your HTML form to handle file uploads. The code above assumes that the uploaded file is namedfilein the form. It first checks if the file was uploaded successfully without any errors and then generates a unique filename. Finally, it moves the uploaded file to the specified destination directory.